docs

v0.1.2 문서를 보고 있습니다. 이 버전은 더 이상 지원하지 않습니다. 최신 릴리스: v0.1.3 →

#E0136: with 뒤에도 간직한 스레드 로컬 값

ThreadLocal의 with는 호출하는 동안만 값을 클로저에 빌려 줍니다. 클로저가 그 값을 간직하거나, 돌려주거나, 넘기거나, 그것을 가진 채 await하면 값이 호출보다 오래 살거나 다른 스레드로 옮겨집니다. 클로저 안에서 쓰고, 밖으로는 .clone()이나 계산한 결과를 꺼내세요.

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`

복사본을 돌려주세요.

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

관련 코드: E0106 잠금이 풀린 뒤에 쓴 값, E0105 넘긴 뒤에 쓴 값

잘못된 곳이 있나요? GitHub에서 이 페이지 고치기. 마지막 수정