Rust in Pieces

Syed Suhail Ahmed

May 26th, 2016 Tech Talk  

A developer’s constant eagerness to explore and learn new languages keeps good engineering teams on the edge.

After I mastered Javascript and Python, I wanted to explore Rust. My familiarity with C++ and how Rust differentiates itself from other languages was enough to make me eager to get my hands dirty.

Memory Management

Memory Management

The major thing that differentiates Rust from other languages is its unique method of solving the memory management problem. Languages like Java and Python use automatic memory management in the form of garbage collection and the developer doesn’t have to worry much about managing memory for a majority of the applications.

In languages like C and C++, the developer is responsible for managing memory. This also means developer mistakes can lead to memory leakages.

Rust tries to stay on the middle ground. Memory safety is provided by Rust through the ownership system. It is owned by variables and can be temporarily borrowed by other variables. This allows Rust to achieve memory safety at compile time without relying on garbage collection, or zero cost abstractions.

One of the things I found quite annoying about Rust is that it is too syntax-heavy. There is a lot of syntax involved while writing non-trivial Rust code. You’d probably make use of all the keys on your keyboard by the end of your program.

Another obstacle I faced was the learning curve. I think the learning curve is too steep (Or is it too flat? Check this for details). A lot of concepts are hard to learn especially if you are coming from higher-level languages. Also, since this is a recent language, more features will be added, making it harder to learn.

Variable Bindings are Immutable by Default

Like other languages, Rust lets you declare variables. The value is bound to a name. But unlike many languages, variable bindings are immutable by default. The languages I have used so far allow binding of values to a name and that value can be changed at any time. But in Rust, if you assign a value to a name then it cannot be changed at all unless you explicitly specify it.

For example, I can assign a value to a variable such as:
let x = 32;

But after that, if i try:
x = 64;

the rust compiler will display an error:

error: re-assignment of immutable variable `x` [E0384]

If you want to have a mutable variable, you have to explicitly define the variable as such. For example:

let &mut x = 32;
x = 64;

Types and Traits

Rust is a statically-typed language. Coming from a dynamically-typed language I found this new and interesting. Early in my journey, I found the combination of static types and immutability annoying as it was restrictive. But as I went further, I found it to be a safety net for all the memory errors I might make.

Traits tells the compiler about the functionality a type must provide. It is similar to interfaces in other languages, but new traits can be implemented for existing types also.

Overall, I liked using Rust and I think it has good scope as a language. I’ll keep using it for System Code and low-level performant code to get to know it better. I look forward to trying it out in many other situations as well.

  • creatonez

    It’s not like you need mutable bindings most of the time. Immutable by default is great for writing predictable and more functional (most functional languages have immutable-by-default bindings) code that just works when you introduce multithreading. Rust basically makes it so “shared” and “mutable” are mutually exclusive. If something is shared, it is not mutable. Check out Steve Klabnik’s talk on concurrency on rust:

    Also, if you didn’t know, in rust you can use the same binding name twice:

    let something = 5;
    let something = something + 4;

    This is perfectly legal and is common practice since `something` isn’t actually mutable here, the binding’s name is just re-used.

    • Syed Suhail Ahmed

      Thanks for the link to the amazing talk. Yes. I totally agree with you regarding the immutable by default feature. The point why I mentioned mutable bindings was to highlight such a feature provided by Rust