本頁尚未翻譯,以下為英文原文。
#E0105: value used after it was handed over
The value was moved into a lock, sent through a channel, handed to a task (as task::spawn_blocking_with_arg's argument, or as a spawned future), or moved into a closure with [move x], and is used again here; its new owner may be changing it. Pass .clone() to keep a separate copy, or give the variable a new value first.
let xs = [1, 2, 3];
let m = Mutex::new(xs);
xs.push(4);
error[E0105]: `xs` was moved into the `Mutex` at line 2 --> main.nabi:3:1 | 2 | let m = Mutex::new(xs); | -------------- `xs` moved into the `Mutex` here 3 | xs.push(4); | ^^ | = help: it belongs to the `Mutex` now; use it through the lock: `using g = m.lock() { g.get().push(4) }`, or hand over `xs.clone()` to keep a separate copy
Use the value through the lock, or hand over xs.clone() if you need your own copy:
let xs = [1, 2, 3];
let m = Mutex::new(xs);
using g = m.lock() { g.get().push(4) }
using g = m.lock() { println(f"{g.get().len()}") }
4
Nabi has no move semantics, so xs still refers to the same object; using it afterwards would race with the new owner. The check follows copies, fields, collection elements, closure captures and calls. Chapter 12 shows more cases.
Related: E0106 value used after its lock was released, E0107 shared value handed over, E0133 resource used after it was moved or closed
Found a mistake? Edit this page on GitHub. Last updated