E0001
Assignment to something that cannot change
ErrorSafe fixOnly a variable, declared with let mut, a mut field, or an element of an
array held in one of them can be assigned. A let binding, a parameter, a
binding made by for, if let or while let, a field without mut and a
constant keep the value they were given. This holds for = and for compound assignments
such as +=.
Why
Sisu marks everything that can change with mut
(ADR 0011), so a reader knows
that a binding or field without it keeps its value. A parameter keeps the value
it was passed, so assigning to one never changes the caller's value.
Broken
fn main() {
let total = 0
total = 5
print(total)
}
What sisu says
error[E0001]: cannot assign to `total` --> E0001.sisu:3:5 | 2 | let total = 0 | ----- declared with `let` here 3 | total = 5 | ^^^^^^^^^ cannot assign twice | help: declare it with `let mut` | 2 | let mut total = 0 | +++ sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0001
Fixed
fn main() {
let mut total = 0
total = 5
print(total)
}
Related
- W0002: a
let mutthat is never reassigned.
Sisu