# XO0520: mutation of a copied or stored collection element is never written back

Xo collections hold values, not references. A local `var` initialized from a collection element (`m.get(k)`, `m[k]`, `xs[i]`, `first`, `find`, optionally with `??`, `?`, or `.or_err(...)?`) and then mutated changes only the copy; the collection keeps the old value. The same holds for a `var` mutated after being pushed or put into a collection. Store it again (`m.put(k, b)`), or use `m.update(k, init, fn(var b) { ... })`. Reading or returning the copy does not count as storing. See core 5.2.

## Example

```xo
struct Bucket { tokens: Int }

fn take(m: Map[Str, Bucket], k: Str) {
  var b = m.get(k) ?? Bucket{tokens: 3}
  b.tokens -= 1
}
```

## Fix

```xo
struct Bucket { tokens: Int }

fn take(var m: Map[Str, Bucket], k: Str) {
  var b = m.get(k) ?? Bucket{tokens: 3}
  b.tokens -= 1
  m.put(k, b)
}
```

Run `xo explain XO0520` for this text in a terminal, or see
[section 10.6 of the specification](../../spec/core/#106-diagnostic-codes).

