OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Bindings, mutability and shadowing

Choose between an immutable binding, mutating a value and introducing a new binding.

Lesson 3 of 30 · About 20 minutes · Beginner · English

By the end of this lesson

Choose between an immutable binding, mutating a value and introducing a new binding.

Before you start

  • Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
  • Complete lesson 2, or be comfortable explaining and running its examples.

Three words to know

Binding
A name associated with a value.
Mutability
Permission to change a bound value.
Shadowing
A new binding reusing an existing name.

1. Understand the idea

let introduces a binding. Bindings are immutable unless you write mut. Reassignment changes a mutable binding; shadowing with another let creates a new one and may even change its type. Prefer immutable names when a value should stay fixed: an accidental reassignment then becomes a compiler error. Mutability is a design choice, not a way to make every compiler error disappear.

2. Save, run and observe

Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.

Count and describeRUST · save as lesson.rs
fn main() {
    let mut count = 2;
    count += 1;
    let count = format!("{count} tasks");
    println!("{count}");
}
What you should see · example output
3 tasks
Compile and run on macOS/LinuxTERMINAL · Bash or Windows PowerShell
rustc --edition=2024 lesson.rs -o lesson
./lesson

3. Explain the result

The first count holds an integer and can be increased. The second let shadows it with an owned String from format!. The final binding is immutable. Using count += 1 after shadowing would be an operation on a string, not on the original integer.

YOUR TURN

A small check before you continue

Remove mut but keep count += 1. Should this compile?

Show a suggested answer

No. Reassignment requires a mutable binding. Either retain mut or replace the increment with a new let binding.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.