Statics in Rust
A static lifetime means the value will exist until the end of the program, so any value with a static lifetime can be shared across threads.
Any variable declared static exists before main and remains in scope until the program ends. Values can also be leaked on the heap to guarantee they exist for the program's entire duration.
'static is a lifetime that says the value will exist till the end of the program, so any value with a static lifetime can be shared across threads.
static X: [i32; 3] = [1, 2, 3];
thread::spawn(|| dbg!(&X));
thread::spawn(|| dbg!(&X));
Any variable defined by static exists before main and remains until the program ends.
Leaking with Box::leak
use std::thread;
fn main() {
let x: &'static [i32; 3] = Box::leak(Box::new([1, 2, 3]));
thread::spawn(move || dbg!(x));
thread::spawn(move || dbg!(x));
}
We are moving the static reference using the move keyword, not the value. Box::leak lets us leak a value on the heap, guaranteeing that no one can free it and that it will exist as long as the program runs.
