#E0105: 值移交之后又被使用
这个值已经被移入锁、通过通道发送、交给任务(作为 task::spawn_blocking_with_arg 的实参,或者作为派生的 future),或者用 [move x] 移入闭包,却在这里再次被使用;它的新所有者可能正在修改它。传入 .clone() 以保留一份单独的副本,或者先给变量赋一个新值。
nabi
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
通过锁使用这个值;如果需要自己的副本,就移交 xs.clone():
nabi
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 没有移动语义,所以 xs 仍然指向同一个对象;之后再使用它就会与新所有者产生竞争。这项检查会跟踪副本、字段、集合元素、闭包捕获和调用。更多情况见第 12 章。
相关错误码:E0106 值在其锁释放之后又被使用,E0107 移交了共享的值,E0133 资源在移动或关闭之后又被使用
发现错误? 在 GitHub 上编辑此页 最后更新: