Hello, I'm learning about closures but I have run in to a little bit of a problem with them. When I just store one in a variable as shown below everything works perfectly, however when I want to store it in the struct I'm receive the following error. Can anyone help me digest this error, because it isn't intuitive for me.
1. Open in split
---
error[E0597]: `a` does not live long enough
--> src/main.rs:14:31
|
6 | let a = 1;
| - binding `a` declared here
...
13 | callback: Box::new(|| {
| - -- value captured here
| ___________________|
| |
14 | | println!("{:#?}", a);
| | ^ borrowed value does not live long enough
15 | | }),
| |__________- cast requires that `a` is borrowed for `'static`
16 | };
17 | }
| - `a` dropped here while still borrowed
|
= note: due to object lifetime defaults, `Box<dyn Fn()>` actually means `Box<(dyn Fn() + 'static)>`
Code:
struct C {
callback: Box<dyn Fn()>,
}
fn main() {
let a = 1;
let _var = Box::new(|| {
println!("{:#?}", a);
});
let _struct = C {
callback: Box::new(|| {
println!("{:#?}", a);
}),
};
}