Mutability -

Alright, so we gotta talk about mutability.

Mutability refers to the ability of an object's value to be changed after it's creation. If you can change it, it's considered mutable. If you cannot alter it after its creation, it is considered immutable.

So, by default, when we use variables and store things in them in Rust, they are immutable. There are a lot of reasons why to do this and they often relate to concurrent programming and better memory management.

In concurrent programming they are used because we are preventing the ability of multiple threads or processes to modify the same bit of information. If we could, we can have problems such as deadlocking, and data races.

When we make things immutable, we force each process or thread to make its own copy of the data to work on or modify instead.

In terms of memory management, we dont have to concern oursleves with locking the data, and references to the data can be safely cached, reducing the memory footprint of an application. We can reuse objects, and not get into a situation where we are using multiple memory allocations for the essentially the same bit of information.

Long story short, we have to say that we want to be able to change the value of x later in our code, and we do that with the mut keyword.


Subscribe to Rust for Rubyists

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.