#E0105: 值在移交後被使用
這個值已移入鎖、經由通道送出、移交給任務(作為 task::spawn_blocking_with_arg 的引數,或作為 spawn 出去的 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 上編輯此頁 最後更新: