I am trying to use an object mechanics
from the higher scope in a callback. To do that I tried to make object mechanics
static.
pub fn main()
{
let mut mechanics : &'static Mechanics = &'static mut Mechanics { val : 13. };
let mut renderer = Render::new( "solution1".to_string() );
renderer.f_on_update = Box::new( ||
{
println!( "mechanics.val : {}", mechanics.val )
});
renderer.update();
}
//
struct Mechanics
{
val : f32,
}
//
struct Render
{
name : String,
f_on_update : Box< dyn Fn() >,
}
//
impl Render
{
fn new( name : String ) -> Self
{
let f_on_update = || {};
Self { name, f_on_update : Box::new( f_on_update ) }
}
fn update( &self )
{
(self.f_on_update)();
}
}
But I get the error:
error: borrow expressions cannot be annotated with lifetimes
--> src/static_solution_1.rs:4:44
|
4 | let mut mechanics : &'static Mechanics = &'static mut Mechanics { val : 13. };
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| annotated with lifetime here
| help: remove the lifetime annotation
Changing to
let mut mechanics : &'static Mechanics = &Mechanics { val : 13. };
Gives the error:
error[E0597]: `mechanics` does not live long enough
--> src/static_solution_1.rs:8:37
|
6 | renderer.f_on_update = Box::new( ||
| - -- value captured here
| __________________________|
| |
7 | | {
8 | | println!( "mechanics.val : {}", mechanics.val )
| | ^^^^^^^^^ borrowed value does not live long enough
9 | | });
| |____- cast requires that `mechanics` is borrowed for `'static`
10 | renderer.update();
11 | }
| - `mechanics` dropped here while still borrowed
I do something wrong but have no clue where to look for an answer. Reading about static lifetime didn't help me to overcome the obstacle. Any suggestion?