All about Rust
the programming language, not the game

03 August 2016

The subtitle of this post is inspired by the many times that I typed “How do I x in Rust” into Google, and had it give me results for how to manipulate strings in the game. When you’re searching for Rust stuff, be sure to include other keywords to make it clear you’re interested in programming.

So, what is Rust? According to “The Rust Programming Language” (a good online book about Rust which can be read at https://doc.rust-lang.org/book/), Rust is “a systems programming language focused on three goals: safety, speed, and concurrency”. A less formal description is that it’s trying to be a modern replacement for C or C++, with more compile time checks and less footguns.

Avoiding Accidents with References

To compete with C, Rust tries to be high performance and have a small memory footprint. To have a small memory footprint, it opts to use manual memory management rather than a garbage collector. The reason that manual memory management can be such a pain is that there are so many ways that you can do it wrong.

For me the killer feature of Rust is that most of these problems with managing memory in Rust are brought forward to compile time. Take this piece of code as an example:

//Create a new vector with three elements
let mut myList = vec!(1, 2, 3);

//for each item in the vector
//.enumerate() adds the index to the items passed in
for (i, item) in myList.iter().enumerate() {
    if *item < 2 {
        myList.remove(i);
    }
}

Do you see the bug there? It’s one I’ve written more times that I should have. Generally speaking, changing a list while you’re iterating over it can cause errors like items in the list being skipped or handled twice. If you’re iterating using a pointer, changing the list could result in invalidating your pointer. In short, it’s bad news, but most languages will let you do it because the language itself doesn’t know its dangerous. Rust will refuse to compile it.

error: cannot borrow `myList` as mutable because it is also borrowed as immutable [E0502]
    myList.remove(i);
    ^~~~~~
note: previous borrow of `myList` occurs here; the immutable borrow prevents subsequent
moves or mutable borrows of `myList` until the borrow ends
    for (i, item) in myList.iter().enumerate() {
                     ^~~~~~
previous borrow ends here
    for (i, item) in myList.iter().enumerate() {
        if *item < 2 {
            myList.remove(i);
        }
    }
    ^

As the error says, as soon as you’re outside of the loop you can do whatever you want to that vector again. I trimmed the compiler output to fit better in this post, the original also had line numbers and file names.

This might not seem mindblowing in a trivial example like this, but Rust will catch this even if the iteration and mutation of the vector are separated by a few layers of function calls.

Pattern Matching to Victory

Another thing that can go funny while programming is updates that need to be made in multiple places, but aren’t. Say you have the following enum

enum MyDodgyEnum {
    Foo,
    Bar
}

and some code using it

fn map_to_int(input: MyDodgyEnum) -> i32 {
    match input {
        MyDodgyEnum::Foo => 1,
        MyDodgyEnum::Bar => 2
    }
}

This compiles and works great. One day, you decide to update the dodgy enum to add a new value.

enum MyDodgyEnum {
    Foo,
    Bar,
    FooBar
}

You were supposed to also update your mapping function, but you forgot. Rust will refuse to compile it (there is a default case if you only want to match those specific two cases).

error: non-exhaustive patterns: `FooBar` not covered [E0004]
    match input {
        MyDodgyEnum::Foo => 1,
        MyDodgyEnum::Bar => 2
    }

Error handling in Rust is actually done entirely through return types (there are no exceptions in Rust), so this compile time checking makes sure that you at the very least acknowledge potential error states, even if you’re just passing them up the stack.

Where can I Learn More?

This has just been a really quick post to let me write some examples of why Rust is awesome. If you’re interested in learning Rust, I strongly recommend checking out the book on the Rust website, https://doc.rust-lang.org/book/.