本頁尚未翻譯,以下為英文原文。
#E0058: invalid closure capture
A closure's capture list ([ref x]) names something it cannot capture: no variable of that name, or a ref capture in a closure that may run on another thread. Capture a let variable or parameter, and share across threads through a Mutex or an atomic.
nabi
let total = 0;
let add = [ref totl] |x: i32| { total += x; };
error[E0058]: `[ref totl]` names no variable in scope --> main.nabi:2:11 | 2 | let add = [ref totl] |x: i32| { total += x; }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Capture the variable by its name:
nabi
let total = 0;
let add = [ref total] |x: i32| { total += x; };
add(2);
println(f"{total}");
2
Related: E0085 value cannot cross threads, E0057 invalid assignment
Found a mistake? Edit this page on GitHub. Last updated