Rust Variables
In Rust, variables are immutable by default. Add mut to allow changes. This is a design choice that makes programs easier to reason about.
ExampleRust
fn main() {
let name = "Ada";
let mut age = 36;
println!("{} is {}", name, age);
age = 37;
println!("{name} is now {age}");
const YEAR: u32 = 2026;
println!("{}", YEAR);
let x = 5;
let x = x * 2; // shadowing: a new variable with the same name
println!("{}", x);
}Output
Ada is 36 Ada is now 37 2026 10
If you try to change a variable without mut, the compiler stops you with an error. Shadowing (let x = x * 2) creates a new variable rather than changing the old one.
Quick check
Are Rust variables mutable by default?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.