let / let mut
Try it yourself →Variable definition; immutable by default.
Syntax
let name = value; · let mut name = value;
Description
Variables defined with let are immutable by default. To make them mutable, let mut is required — Rust's safety philosophy.
Example
fn main() {
let x = 5;
let mut y = 10;
y = 20;
println!("{} {}", x, y);
}Expected output
5 20
When to use?
Storing all kinds of data.
Common mistakes
- ⚠️ Modifying without mut is an ERROR: let x = 5; x = 6; → fails to compile.