编译以下代码时:
fn main() {
let mut fields = Vec::new();
let pusher = &mut |a: &str| {
fields.push(a);
};
}
编译器给我以下错误:
error: borrowed data cannot be stored outside of its closure
--> src/main.rs:4:21
|
3 | let pusher = &mut |a: &str| {
| ------ --------- ...because it cannot outlive this closure
| |
| borrowed data cannot be stored into here...
4 | fields.push(a);
| ^ cannot be stored outside of its closure
在Rust的后续版本中:
error[E0521]: borrowed data escapes outside of closure
--> src/main.rs:4:9
|
2 | let mut fields = Vec::new();
| ---------- `fields` declared here, outside of the closure body
3 | let pusher = &mut |a: &str| {
| - `a` is a reference that is only valid in the closure body
4 | fields.push(a);
| ^^^^^^^^^^^^^^ `a` escapes the closure body here
这个错误的含义是什么,我该如何修复我的代码?