keyword

Rust mut: allow a binding to change

Use `mut` when a local variable needs to be reassigned. This is useful for counters and running totals. Here the count increases when a new task arrives. Expected output: 3 Why: The compound assignment adds one to the current count and stores the result. Common mistake: Mutability does not allow changing the binding to an incompatible type.

Formal Syntax & Structure

rust syntaxrust
let mut name = expression;

Working Example

Example Snippetrust
fn main() {    let mut tasks = 2;    tasks += 1;    println!("{tasks}");}
Rust Reference