本页尚未翻译,以下为英文原文。
#E0057: invalid assignment
The left side of = cannot be assigned: a const, a static, a variable not declared with let, or a value captured by a closure without [ref name]. Assign to a let variable, a field of one, or an element of one. A left side that is a value, such as a call result, is E0140. A field set on a copy that nothing reads afterwards (a by-value self or parameter, a closure's argument, a for binding, a let of xs[i]) is lost, and an error too: take ref self or a ref parameter, return the argument from the closure and use the result, write for ref x in xs, or write the element back.
let count = 0;
coutn = count + 1;
error[E0057]: `coutn` is not declared; a variable is created only by `let` --> main.nabi:2:1 | 2 | coutn = count + 1; | ^^^^^ | = help: did you mean `count`? To declare a new variable, write `let coutn = count + 1;`
Assign to the variable you declared, or declare a new one with let:
let count = 0;
count = count + 1;
println(f"{count}");
1
Nabi has no declaration by assignment: a variable is created only by let (5.1). The same holds for each name in a destructuring assignment such as (a, b) = (b, a);: declare them with let first, or all at once with let (a, b) = ..;.
A field set on a copy that nothing reads afterwards is this error too: the change would be lost. A struct is a value, so a by-value self or parameter, a closure's argument (m.with(|a| { a.n += 1; })), a for binding and a let of an element (let q = xs[i];) hold copies (2.3.8).
struct Point { x: i32; y: i32; }
let ps = [Point { x: 1, y: 0 }, Point { x: 2, y: 0 }];
for p in ps { p.x = 7; }
error[E0057]: `p.x` is set on a copy of the element, so `ps` does not change --> main.nabi:4:15 | 4 | for p in ps { p.x = 7; } | ^^^ | = help: bind each element as a place with `for ref`: `for ref p in ps { p.x = 7; }`
for ref binds each element as a place, so the write reaches the list (3.13.3). In a method, take ref self; for a parameter, ref p; in a closure, return the argument and use the result; for a let of an element, write it back (xs[i] = q;).
struct Point { x: i32; y: i32; }
let ps = [Point { x: 1, y: 0 }, Point { x: 2, y: 0 }];
for ref p in ps { p.x = 7; }
println(f"{ps[0].x} {ps[1].x}");
7 7
Related: E0027 cannot find value or function, E0038 variable named like a nested function, E0140 assignment to a value that is not a place
Found a mistake? Edit this page on GitHub. Last updated