docs

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

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

#E0136: thread-local value kept past with

A ThreadLocal's with lends the value to its closure only for the call. The closure keeps it, returns it, hands it over or awaits with it, so it would outlive the call or move to another thread. Use it inside the closure, and take a .clone() or what you computed from it out instead.

nabi
use std::thread;

static TL: thread::ThreadLocal<List<i32>> = thread::local(|| -> List<i32> { [1, 2, 3] });

let xs = TL.with(|l| l);
error[E0136]: this returns the value `with` lends out of its closure: the value is only lent to it
  --> main.nabi:5:22
   |
 5 | let xs = TL.with(|l| l);
   |                      ^
   |
   = help: return a copy (`.clone()`) or what you computed from it, or take a `ThreadLocal`'s value out with `replace` / `take`

Return a copy:

nabi
use std::thread;

static TL: thread::ThreadLocal<List<i32>> = thread::local(|| -> List<i32> { [1, 2, 3] });

let xs = TL.with(|l| l.clone());
xs.push(4);
println(f"{xs.len()}");
4

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

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