A closure gets a copy of the variables it captures, so assigning to one would silently change
only the copy. Auk rejects it; use a for loop, or build the result with reduce.
Wrong#
fn main() {
var total = 0
[1, 2].map(x => { total = total + x })
print(total)
}Fixed#
fn main() {
var total = 0
for x in [1, 2] { total += x }
print(total)
}Common causes#
- Accumulating inside
maporeach: use aforloop, orreduce/sum. - A counter updated from a callback: have the callback return the new value, or use a
forloop.