Programs need to remember things: the topic you are studying, how many minutes you have planned, how many sessions are done. A binding gives a value a name so you can use it later. let topic = "Rust"; binds the name topic to the text "Rust". From then on, writing topic means that value, and a format string can use {topic} to put it into the output.
Rust makes bindings immutable by default: once a name has a value, it keeps that value. This is a deliberate choice. When you read a long program and see let goal = 60;, you know goal is still 60 at the bottom without checking every line in between. When a value really does need to change, you say so with let mut. The word mut (short for mutable) is a signal to readers and to the compiler: watch this one, it changes.
Predict and run
minutes += 5 is short for minutes = minutes + 5: take the current value, add five and store the result back in the same binding. Predict the final value of minutes and the full sentence.
fn main() {
let topic = "Rust";
let mut minutes = 20;
minutes += 5;
println!("{topic}: {minutes} minutes");
}Check your prediction
Rust: 25 minutesThe line minutes += 5 ran before println!, so the placeholder shows the updated value. Order matters: move the println! above the addition and predict what it prints (20). Then remove mut and run. The compiler refuses:
fn main() {
let minutes = 20;
minutes += 5;
println!("{minutes}");
}It reports error[E0384]: cannot assign twice to immutable variable minutes and points at the += line, with the help consider making this binding mutable. The let line only declares; the line that changes the value is the one that needs permission.
Shadowing: a new binding with an old name
There is a second way to get a different value under the same name. Writing let again creates a brand-new binding that hides, or shadows, the earlier one. Nothing is changed in place; the old binding is simply no longer reachable by that name.
fn main() {
let minutes = 20;
let minutes = minutes + 5;
println!("After the warm-up: {minutes}");
let minutes = minutes * 2;
println!("Two sessions: {minutes}");
}After the warm-up: 25
Two sessions: 50No binding here is mutable. Each let minutes reads the previous minutes on the right side, then introduces a new one. Use mut when a value is updated repeatedly, such as a running count. Use shadowing when you compute a new version of a value once and never need the old one again.
What a placeholder can hold
An inline placeholder like {minutes} accepts only a plain name. A calculation does not fit inside the braces:
fn main() {
let minutes = 20;
println!("{minutes + 5}");
}This fails with error: invalid format string: expected }, found +. Put the calculation after the text and use an empty {} placeholder instead, as in the first lesson. You can mix both styles in one line.
fn main() {
let topic = "Rust";
let minutes = 20;
println!("{topic}: {} minutes with a break", minutes + 5);
}Rust: 25 minutes with a breakStart without mut
Write let first. If the compiler asks for mut, check that the change is really what you meant, then add it. Bindings that never change are easier to trust.
Practice
Your turn
Start a counter at 2 sessions. Add 3 and print Sessions: 5 using the counter's name in the format string. On a second line, print Minutes: 125, calculated as the number of sessions times 25 with a {} placeholder.
fn main() {
let sessions = 2;
// Update sessions, then print both lines.
println!("Sessions: {sessions}");
}Hint 1: the idea
The count changes after it is created, so the binding needs to allow reassignment. The multiplication cannot go inside the braces.
Hint 2: a first step
Add mut before the name sessions, then write sessions += 3; before the first println!.
Worked solution
fn main() {
let mut sessions = 2;
sessions += 3;
println!("Sessions: {sessions}");
println!("Minutes: {}", sessions * 25);
}Sessions: 5
Minutes: 125Both lines use the value after the addition, because the update ran first. Shadowing would also work here (let sessions = sessions + 3;), since the count changes only once. In the next lesson you look more closely at the numbers themselves and what happens when you divide them.
Completion is saved in this browser only. It does not measure mastery.