I need to access and modify a static variable:
mod my_mod {
use std::collections::HashMap;
pub enum MyA<'a> {
Instance(&'a MyC<'a>),
Another,
}
pub struct MyC<'a> {
field: &'a str,
}
pub struct MyB<'a> {
map: HashMap<i32, MyC<'a>>,
}
impl<'a> MyB<'a> {
pub fn new() -> Self {
MyB {
map: HashMap::new(),
}
}
pub fn insert<'b: 'a>(&'b mut self, k: i32) -> MyA {
let v = MyC { field: "hello" };
self.map.insert(k, v);
MyA::Instance(self.map.get(&k).unwrap())
}
}
}
use crate::my_mod::{MyA, MyB};
use once_cell::sync::Lazy;
use std::sync::Mutex;
static MYB: Lazy<Mutex<MyB>> = Lazy::new(|| Mutex::new(MyB::new()));
fn my_func() -> i32 {
let mut my_b = MYB.lock().unwrap();
let my_a = { my_b.insert(1) };
match my_a {
MyA::Instance(s) => 1,
MyA::Another => 2,
}
}
fn main() {
my_func();
}
My dependencies:
[dependencies]
once_cell = "1.8.0"
The error:
error[E0597]: `my_b` does not live long enough
--> src/main.rs:38:18
|
38 | let my_a = { my_b.insert(1) };
| ^^^^----------
| |
| borrowed value does not live long enough
| argument requires that `my_b` is borrowed for `'static`
...
43 | }
| - `my_b` dropped here while still borrowed
It tells me that my_b
is dropped at the end of my_func
but the reference needs to be valid for the whole program run, if that is what 'static
means. Why does it have to be valid for 'static
? Is it a problem with lifetime parameters?