Skip to content
TE004 · Type · error

Cannot reassign immutable binding

`let` bindings are immutable by default. Use `let mut x = ...` to allow reassignment, or use a fresh `let` shadow.

Why this fires

let creates an immutable binding. Reassigning it is a type error:

let count = 0
count = count + 1
// ^^^^^ error: cannot reassign immutable binding `count`

Immutability is the default because most local bindings don't change after they're set, and the discipline catches a category of bugs where a value mutates somewhere unexpected.

Fix it

1. Make it mutable

If the binding really does need to change, declare it with let mut:

let mut count = 0
count = count + 1

2. Re-bind with a fresh let (shadowing)

If you're transforming a value rather than tracking state, prefer shadowing — each let creates a new binding, leaving the old one unreachable:

let count = 0
let count = count + 1     // a new binding, shadows the old one
let count = count.to_str()

Shadowing is idiomatic for staged transformations (parse → normalize → validate) because each stage's type can differ.

3. Refactor with match or expressions

In many cases the apparent need to reassign disappears once you express the same logic as a match or pipeline:

let result = match input {
    Ok(v)  => process(v),
    Err(e) => default_for(e),
}

See also

See also

enespt-br