docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

本頁尚未翻譯,以下為英文原文。

#E0107: shared value handed over

The value is handed to another owner while other code can still reach it (a lock, a thread-local, a Sync object, or another argument of the same call). Hand over a .clone(), or a value nothing else holds.

nabi
let m = Mutex::new([1, 2, 3]);
let (tx, rx) = channel::<List<i32>>();
using g = m.lock() {
    try! tx.send(g.get());
}
error[E0107]: `g.get()` can't be sent through a channel: the `Mutex` behind `g` still holds it
  --> main.nabi:4:18
   |
 4 |     try! tx.send(g.get());
   |                  ^^^^^^^
   |                  ------- taken out of the lock here
   |
   = help: hand over a deep copy (`g.get_clone()`), or take it out of the lock first (`g.set(..)` a new value)

Send a copy:

nabi
let m = Mutex::new([1, 2, 3]);
let (tx, rx) = channel::<List<i32>>();
using g = m.lock() {
    try! tx.send(g.get_clone());
}
println(f"{(rx.recv() ?? []).len()}");
3

Related: E0105 value used after it was handed over, E0106 value used after its lock was released

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