我不明白为什么函数不与上层块共享内存,而任意块和lambda共享内存。
让我们考虑一下这段代码:
fn main() {
let trigger: u32 = 4;
{{{ 4 == trigger }}}; // works
let lambda = |c: &u32| c == &trigger; // works
fn func(c: &u32) -> bool {
c == &trigger // error
}
}
编译器会产生此错误
error[E0434]: can't capture dynamic environment in a fn item
--> src/main.rs:10:9
|
10 | c == &trigger // error
| ^^^^^^^
|
= help: use the `|| { ... }` closure form instead
为什么func
无法到达trigger
?