Camp 1 · Step 3 of 12
Variables and mutability
let, mut, and shadowing — Rust flips the default: everything is constant until you say otherwise.
Most languages let variables change freely and offer const as an
opt-in. Rust inverts it: immutable by default, changeable only by
explicit request. This one inversion prevents a shocking number of bugs.
let: immutable by default
Uncomment camp = 2 and the compiler delivers one of its famous
teaching-errors:
error[E0384]: cannot assign twice to immutable variable `camp`
help: consider making this binding mutable: `mut camp`It names the problem and hands you the fix.
mut: opting into change
let mut announces "this value will change" — to the compiler and to
every human reader. Scanning Rust code, the muts show you exactly where
the moving parts are.
Shadowing: the third option
Rust lets you re-let a name — even changing its type:
Each let creates a new variable that shadows the old one. Idiomatic
for transform pipelines: parse the text, keep the name.
What does Rust do when you assign to a plain let variable twice?
What distinguishes shadowing (let x = …; let x = …;) from mutation?