Coding/Learn/rust/variables and mutability
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnVariables, Mutability & Shadowing
3 / 20 0%
Lesson 03 of 20
6 mins read

Variables, Mutability & Shadowing

PreviousNext Lesson
In Plain English

In Rust, variables are immutable by default. To allow reassignment, you must explicitly declare the variable with the mut keyword. Shadowing allows re-declaring a variable with let to transform types cleanly.

The Mental Model

"In Rust, a variable without mut is like stone carving—once engraved, it cannot be altered. Adding mut is like using an erasable whiteboard."

Deep Dive: How It Works

Immutable by Default: Prevents unintended state mutations across large concurrent codebases.

mut Keyword: let mut count = 0; explicitly marks intent to mutate.

Shadowing: let x = 5; let x = x + 1; creates a new variable reusing the same identifier, allowing type transformation.

Syntax Blueprint

let x = 5; (Immutable) | let mut y = 10; y = 20; (Mutable)

Use let for read-only values, mut for values that change, and shadowing for type changes.

PointCore Rules to Remember

Point star
Safety by Default: Immutability prevents race conditions and accidental variable reassignment.
Point star
mut Keyword: Required on any variable binding that requires modification.
Point star
Shadowing: Re-declaring with let lets you change type (e.g. string to parsed integer) without new names.

Common Beginner Traps & How to Fix Them

Mistake:Reassigning an immutable variable.

Why it happens: let x = 5; x = 6; causes compiler error E0384: cannot assign twice to immutable variable.

How to fix: Declare with let mut x = 5; if reassignment is intentional.

Pro Tip: Shadowing is cleaner than inventing throwaway names like input_str and input_int—just use let input = input.trim().parse()?;.

Live Interactive Example

Hit Run Code to see it live
Mutability and Shadowing in Action
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates mutable state updates and shadowing string to integer length.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Update mutable counter

  • PointDeclare let mut energy = 100;
  • PointSubtract 30 from energy (energy -= 30);
  • PointPrint "Remaining Energy: 70".
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates