Skip to main content

E0004: Assignment to Immutable Variable

You tried to assign a new value to a variable that was not declared as mutable.

Example

fn main() {
let x = 5;
x = 10; // Error: cannot assign to immutable variable `x`
}

How to fix

Declare the variable with mut if you need to reassign it:

fn main() {
let mut x = 5;
x = 10; // OK
}