Error codes

E0323: closure assigns to a captured variable

In a terminal: auk explain E0323 prints this page.

On this page

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#

auk
fn main() {
  var total = 0
  [1, 2].map(x => { total = total + x })
  print(total)
}

Fixed#

auk
fn main() {
  var total = 0
  for x in [1, 2] { total += x }
  print(total)
}

Common causes#

  • Accumulating inside map or each: use a for loop, or reduce/sum.
  • A counter updated from a callback: have the callback return the new value, or use a for loop.

Stuck on a word? Try the glossary. Got a compiler error? Look up its code. Improve this page