What do you mean by "what's happening with them"? In the code you presented so far, all that happens with the closures is that they get stored in your data structure.
Compiling playground v0.0.1 (/playground)
error[E0515]: cannot return value referencing temporary value
--> src/main.rs:7:5
|
7 | A { closure: &move || println!("Foo {x}") }
| ^^^^^^^^^^^^^^---------------------------^^
| | |
| | temporary value created here
| returns a value referencing data owned by the current function
For more information about this error, try `rustc --explain E0515`.
error: could not compile `playground` due to previous error
This works, in turn:
struct A<'a> {
closure: &'a dyn Fn(),
}
fn main() {
let a = {
let x = 3;
A { closure: &move || println!("{x}") }
};
(a.closure)();
}
That code doesn't seem right to my eyes for some reason.
I tried to store it as Fn() -> Return instead of &'a dyn Fn() -> Return but complier throw an error as closure goes out of scope when the method back ends (which makes sense).
When a closure captures nothing, it can be converted to a bare function, which in turn means that it can be promoted to have the static lifetime, hence references to it can be valid forever. You can search for "rust static promotion" for the details and exact rules.