Sub topic 2
Why bindings are immutable by default
This chapter assumes you can run Rust programs with cargo or rustc, know what variables, functions, and loops are, and can read a simple compiler error. You have already seen let declare a binding. Here you learn what "immutable by default" means, what the compiler enforces, and why the language works this way.
What the compiler enforces
A binding created with let cannot be reassigned after it receives a value. The compiler rejects the program instead of letting it run.
fn main() {
let x = 5;
println!("x = {x}");
x = 6;
println!("x = {x}");
}This fails with error[E0384]: cannot assign twice to immutable variable. The check happens at compile time, so no program with this mistake ever runs.
Immutability is a property of the binding, not of the value. The name x is permanently tied to its first value. That distinction matters in later chapters, where a type can allow changes even through an immutable binding.
Delayed initialization
An immutable binding does not need a value on the line where it is declared. You can declare it first and assign it exactly once later.
fn main() {
let x = 5;
println!("x = {x}");
let total;
total = x + 1;
println!("total = {total}");
}The rule is "assigned at most once, and never read before it is assigned." The compiler tracks this through the control flow. Both branches of an if can each assign the binding once, and that is accepted.
fn main() {
let label;
let score = 72;
if score >= 50 {
label = "pass";
} else {
label = "fail";
}
println!("{label}");
}Reading label before every path has assigned it gives error E0381, "used binding isn't initialized".
Why the default is immutable
The language designers chose this default for several reasons.
- Easier reading. If a name never changes, you can read its declaration and know its value everywhere it is used. You do not need to scan the rest of the function for reassignments.
- Fewer bugs. An accidental reassignment, such as a typo that reuses a name, becomes a compile error instead of a wrong result at runtime.
- Intent is visible. When you later see
mut, you know that value is meant to change. The keyword marks the places that need more attention. - Safer shared access. Rust's ownership rules, covered in a later tutorial, depend on knowing which values can change. Many readers of a value are safe only if nobody is changing it. Immutable bindings make that easy to check.
- Optimization. The compiler has more information about which values stay fixed.
The cost is small. When you do need change, you add one keyword. The default simply makes you decide on purpose.
Common errors
| Mistake | Compiler message | Fix |
|---|---|---|
| Reassigning an immutable binding | `E0384` cannot assign twice to immutable variable | Add `mut`, or use a new binding |
| Using `+=` on an immutable binding | `E0384` | Same as above |
| Reading a declared but unassigned binding | `E0381` used binding isn't initialized | Assign on every path first |
| Assigning in a loop to a binding declared without `mut` | `E0384` | The loop can assign more than once, so the binding needs `mut` |
The compiler's help text usually suggests the fix, for example "consider making this binding mutable: mut x". Read that note first.
A habit to build
Prefer computing a value in one expression and binding it once.
fn main() {
let temperature = 31;
let advice = if temperature > 30 { "stay inside" } else { "go out" };
println!("{advice}");
}Here advice never needs mut, because if is an expression that returns a value. Many loops and branches that seem to need mutation can be written this way. The next chapter covers mut for the cases where change is really what you want.
Exercise
The code below does not compile. Why, and what are two different ways to fix it? let count = 0; count = count + 1; println!("{count}");
Show answer
count is immutable, so the second assignment triggers E0384 (cannot assign twice to immutable variable). One fix is to declare it with let mut count = 0;. Another is to bind the result to a new name, for example let count = 0; let next = count + 1; println!("{next}");, or to write let count = 0 + 1; if only the final value is needed.
Exercise
Is this program accepted? Explain why. let label; let n = 3; if n > 1 { label = "big"; } println!("{label}");
Show answer
No. The if has no else, so there is a path where label is never assigned, and reading it gives E0381. Adding an else branch that assigns label once makes the program compile.