Rust by Example

4.1 Mutability

Variables are immutable by default, but this can be overridden using the mut modifier.

fn main() { let _immutable_variable = 1; let mut mutable_variable = 1; println!("Before mutation: {}", mutable_variable); // Ok mutable_variable += 1; println!("After mutation: {}", mutable_variable); // Error! _immutable_variable += 1; // FIXME ^ Comment out this line }

The compiler will throw a detailed diagnostic about mutability errors.