Rust, shared: the mutability checker

What is the mutability checker?

The Rust mutability checker is a compiler phase that applies cutting edge techniques to enforce rules about whether a value is allowed to be altered, and if so in what ways. Mutability of a value is part of its type signature and follows normal typing rules with respect to assignment and inference.

How to trick the mutability checker?

Mutability of a value is part of its type signature. A fully specified type is permitted to be marked as immutable while still permitting interior mutability. This is known as a partially mutable value. To enable interior mutability it is easiest to use one of the standard library wrapper types to safely mutate interior values. Here is an example of using a simple Cell to wrap a mutable interior value:

let c = Cell::new(3);

There are many types available from the standard library to be used for creating interior mutability. Some, such as Cell or RefCell, can only be used in single-threaded applications. Others, such as Mutex, are thread-safe.