Variables and mutability
let creates a binding between a name and a value, and that binding is immutable unless you write let mut. Immutability is the default in Rust, not an opt-in discipline.
Detailed Explanation
A binding is a name attached to a value. let port = 8080; binds port to 8080. Write port = 9090; afterwards and the program does not compile, because the binding is immutable. To allow reassignment you say so up front: let mut port = 8080;.
Rust's authors did not choose this default for tidiness. It falls out of the ownership system. The borrow checker's core rule is that at any moment a value may have many shared references or exactly one mutable reference, never both. That rule is what eliminates data races at compile time and what prevents a vector from being resized while you hold a pointer into it. The rule only works if mutability is visible in the type system, and the cheapest way to make it visible is to make it explicit at every binding. So mut is not a style preference; it is the annotation the checker reads.
That gives you two practical benefits. Concurrency safety comes first: a value reachable only through shared references cannot be written, so sharing it across threads needs no lock. Local reasoning comes second: when you read a function and see a binding without mut, you know its value cannot change between the line that creates it and the line that uses it, without checking anything in between.
Immutability here means the binding cannot be reassigned and the value cannot be mutated through it. It does not mean the data is frozen forever. Ownership can move to a new mut binding, and interior mutability types such as Cell and RefCell allow controlled mutation through a shared reference. State the rule, then the exception: let means no mutation through this binding, and interior mutability is the documented escape hatch.
Beside let there are two ways to name a fixed value, and learners mix them up.
const | static | |
|---|---|---|
| What it is | A value inlined at each use site | A single variable with a fixed memory address |
| Lifetime | No address of its own | 'static, lives for the whole program |
| Type annotation | Required | Required |
| Mutable form | None | static mut, which is unsafe to touch |
| Typical use | const MAX_RETRIES: u32 = 5; | A large lookup table, or an interface with C |
Prefer const. Reach for static when you genuinely need one address, for example a big table you do not want copied at every use. static mut is a shared mutable global with no synchronisation, so reading or writing it requires unsafe; use OnceLock or a Mutex in a static instead.
Unlike let, both const and static demand an explicit type and an initialiser that can be evaluated at compile time. Inside a function body, let almost always infers the type from the value or from later use, so you annotate only when inference is ambiguous, as with let n: u64 = "42".parse().unwrap();.
What does immutability cost? At runtime, nothing at all. mut produces no instruction and changes no layout; it is erased after type checking. The cost is in design. Code written in an imperative style, where a buffer is threaded through and mutated in many places, fights the borrow checker, and the fix is usually restructuring rather than sprinkling mut. When you find yourself adding mut and clone() together to silence errors, that is a signal the ownership story needs rethinking, not more annotations.
Code Examples
const MAX_RETRIES: u32 = 5; // compile-time value, type annotation required
static APP_NAME: &str = "config-check"; // one fixed address, 'static lifetime
fn main() {
let attempts = 0;
// attempts = 1; // error[E0384]: cannot assign twice to immutable variable `attempts`
let mut attempts = attempts; // a new, mutable binding
while attempts < MAX_RETRIES {
attempts += 1;
}
println!("{APP_NAME} gave up after {attempts}"); // prints: config-check gave up after 5
// Mutability belongs to the binding, so it controls method calls too.
let mut hosts = Vec::new();
hosts.push("db-1"); // push takes &mut self, so `hosts` must be `mut`
println!("{}", hosts.len()); // prints: 1
}fn main() {
let settings = vec![String::from("port=8080")];
// A shared reference cannot mutate, and many may exist at once.
let a = &settings;
let b = &settings;
println!("{} {}", a.len(), b.len()); // prints: 1 1
// To mutate, the binding itself must be mutable.
let mut settings = settings; // ownership moves into a mutable binding
settings.push(String::from("host=localhost"));
println!("{}", settings.len()); // prints: 2
}Common Gotchas
`mut` on the binding, not the type
you write `let mut v: Vec<i32>`, never `let v: mut Vec<i32>`. A separate `&mut` on a reference type means something related but distinct: permission to mutate through that borrow.
An unused `mut` is a warning worth heeding
`variable does not need to be mutable` usually means a refactor left behind mutation that no longer happens. Leaving stray `mut` annotations weakens the signal that `mut` is supposed to carry.
`static mut` is a shared mutable global
every access needs `unsafe`, and two threads touching it is undefined behaviour with no compiler help. Use a `static` holding a `Mutex` or a `OnceLock` instead.
Declare-then-initialise is allowed
`let value;` followed by an assignment on every path through a branch compiles, because the compiler verifies the binding is initialised exactly once before use. It stays immutable afterwards.