Variables and Mutability
In Rust, variables are immutable by default. This means once a value is bound to a variable, it cannot be changed unless explicitly marked as mut
.
Declaring Variables
let x = 5;
println!("x is: {}", x);
To make a variable mutable:
let mut x = 5;
x = 6;
println!("x is now: {}", x);
Using mut
allows the variable’s value to change, which is important when managing state.
Rust encourages immutability to prevent unintended side effects and improve code safety.