Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Learn RustChapter 2: Values

Lesson 2.2 · 8 min

Values that change

A chat program has to count: how many messages so far. A count that cannot change is not much of a count. Rust makes you ask before a name may change, and the compiler explains what happens if you forget.

Adds to ChatterboxA message counter.

Compiler errors you will meet E0384

Try to change a name

Chatterbox should say how many messages have been sent. Start the count at zero and add one when a message arrives. Here is the obvious way to write it.

Assigning a second time to a plain name
fn main() {
let messages_seen = 0;
messages_seen = messages_seen + 1;
println!("messages so far: {}", messages_seen);
}
error[E0384]: cannot assign twice to immutable variable `messages_seen`
 --> src/main.rs:3:5
  |
2 |     let messages_seen = 0;
  |         ------------- first assignment to `messages_seen`
3 |     messages_seen = messages_seen + 1;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut messages_seen = 0;
  |         +++

Read the message. "cannot assign twice to immutable variable" says it plainly. To assign is to give a name a value with =. Immutable means "cannot change". A plain let gives a name that is set once. The compiler even suggests the fix in its help text: make it mutable.

This is a safety feature, not a nuisance. In a long program, a value that can change anywhere is a value you cannot trust anywhere. So Rust starts every name as fixed, and you opt in to change, one name at a time, with the word mut (short for "mutable").

let mut allows the change
fn main() {
let mut messages_seen = 0;
messages_seen = messages_seen + 1;
messages_seen = messages_seen + 1;
println!("messages so far: {}", messages_seen);
}
$ cargo run
messages so far: 2
PredictHow many?
fn main() {
let mut count = 10;
count = count + 5;
count = count - 3;
println!("{}", count);
}

What does this print?

  • 12
  • 15
  • 10
Reveal the answer

The program prints: 12 Each assignment uses the current value. 10 becomes 15, then 15 becomes 12. The name count only ever holds the latest value.

A shortcut, and a different trick

count = count + 1 is so common it has a short form: count += 1. Same meaning, fewer characters.

There is also a second way to "change" a name that is not changing at all. You can write let again with the same name. This is called shadowing: the new name sits in front of the old one and hides it. The old value is untouched; you just cannot reach it by that name any more.

Shadowing: a brand new name that happens to match
fn main() {
let count = 1;
let count = count + 1;
let count = count * 10;
println!("{}", count);
}
$ cargo run
20

Which to use? Reach for let mut when one thing really is a running tally, like a counter. Reach for shadowing when you are working a value through steps and the old version is no longer interesting. A rule of thumb: start with plain let, and add mut only when the compiler asks.

Chatterbox v0.2, step two

The counter goes into Chatterbox. It is called messages_seen: it starts at zero and goes up by one for each message.

Chatterbox v0.2 after lesson 2.2: the whole program (green lines are new in this lesson)
src/main.rs4 new lines
// Chatterbox: a small chat program, built one lesson at a time.
 
/// The first line Chatterbox prints.
fn banner(bot: &str, version: &str) -> String {
format!("{} v{}", bot, version)
}
 
/// The greeting, with a name filled in.
fn greeting(name: &str, bot: &str) -> String {
format!("Hi {}! I'm {}.", name, bot)
}
 
fn main() {
let name = "Sam";
let bot = "Chatterbox";
let version = "0.2";
let mut messages_seen = 0;
 
println!("{}", banner(bot, version));
println!("{}", greeting(name, bot));
println!("Type a message and I will answer.");
 
// The greeting was our first message, so the count goes up by one.
messages_seen += 1;
println!("messages so far: {}", messages_seen);
}
Chatterbox prints (a real run)
Chatterbox v0.2
Hi Sam! I'm Chatterbox.
Type a message and I will answer.
messages so far: 1

Download this version (zip)Unzip, then cargo run inside the folder.

Look at the green lines. let mut messages_seen = 0; makes a counter that is allowed to change. After the greeting, messages_seen += 1; adds one, because the greeting was the first message of the chat (the comment above it says so). The last line prints the count, so the run ends with messages so far: 1. One message is not much of a tally yet. Counting the greeting is only a placeholder rule, there so the counter has something to count until Chatterbox can read messages in chapter 3. From then on it counts only what you type, and the greeting no longer counts.

Try itYour turn: count the arrivals

Make count_after return the starting count plus the number of new messages that arrived. Start from the let mut line already there: the function only has to change count once before handing it back.

Make count_after return the starting count plus the number of new messages that arrived. Start from the let mut line already there: the function only has to change count once before handing it back.

fn count_after(start: i32, arrivals: i32) -> i32 {
let mut count = start;
// add the new messages to count
count
}
 
fn main() {
println!("{}", count_after(0, 3));
}

Hidden tests (3): adds_to_existing from_zero nothing_arrives

This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.

Self-check

Say it out loud, then reveal

Self-checkRetrieval beats re-reading
What does E0384 mean?Reveal
You assigned a second time to a name that was not declared mut. Plain let names are fixed.
How do you allow a name to change?Reveal
Write let mut when you create it.
What is the difference between mut and shadowing?Reveal
mut changes the value held by one name. Shadowing makes a brand new name with the same spelling that hides the old one.

That is lesson 2.2. Tick it off so the course remembers where you are.

Questions people ask

Why is Rust immutable by default?

A value that cannot change is easier to trust when reading code. You opt in to change only where you need it.

What does mut stand for?

Mutable, which means able to change.

Is shadowing the same as changing a variable?

No. It creates a new name. The old value still exists until it goes out of scope.

Learn Rust by building a chat app · Lesson 2.2 · every compiler message above is real output from rustc 1.99.0 (b940084d7 2026-09-28) (edition 2024) · Vibe Engines · 2026