The Rust documentation has a OnceLock::get_mut method that returns a mutable reference. But the following code does not compile. What am I missing?
use std::sync::OnceLock;
#[derive(Clone)]
struct System {
comp: Vec<i32>,
}
static GLOB: OnceLock<System> = OnceLock::new();
impl System {
fn new() -> Self {
Self { comp: Vec::new() }
}
}
fn modify(global: &mut System) {
global.comp.push(42);
}
fn main() {
let sys: Box<System> = Box::new(System::new());
GLOB.get_or_init(|| *sys);
let global: &mut System = GLOB.get_mut().unwrap();
println!("global size is {}", global.comp.len());
modify(global);
println!("global size is {}", global.comp.len());
}
Is it really possible to get a mutable reference? How am I supposed to use get_mut()?