docs

You are reading the docs for v0.1.2. Nabi 0.1.2 is no longer supported. Latest release: v0.1.3 →

#E0140: assignment to a value that is not a place

The left side of = or of a compound assignment (+=, -=, ...) is a value, not a place: a call result (f(), d.entry(k).or_insert(0)), an operator result or a tuple. Assigning to it would change a temporary that is dropped right after. Assign to a variable, a field of one (s.x += 1) or an element (xs[i] += 1). To keep a call's result, bind it first: let v = f(); v += 1;. To count in a dictionary, index with a default: d[k, default: 0] += 1;. A lock guard's get() is a copy too: change the stored value with g.update(|v| { v.x += 1; v }) or g.set(v).

nabi
let counts = Dict::new();
for w in ["a", "b", "a"] {
    counts.entry(w).or_insert(0) += 1;
}
error[E0140]: cannot use `+=` on `counts.entry(w).or_insert(0)`: a call result is not a place, so the change would be lost
  --> main.nabi:3:5
   |
 3 |     counts.entry(w).or_insert(0) += 1;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: index with a default: `counts[w, default: 0] += 1;`

or_insert returns the count, not a slot in the dictionary, so += 1 would add to a copy. Index with a default, which stores 0 first when the key is missing:

nabi
let counts = Dict::new();
for w in ["a", "b", "a"] {
    counts[w, default: 0] += 1;
}
println(f"a={counts["a"]} b={counts["b"]}");
a=2 b=1

The same holds for a field of a call result (p().x += 1) and for an element of a fixed array a call returns (arr()[0] = 5): bind the value to a variable and change that. An element of a List or Dict a call returns is a place, since the collection is shared: rows()[0] += 1 changes the list rows() returns (3.4.1).

Related: E0057 invalid assignment

Found a mistake? Edit this page on GitHub. Last updated