Does LazyLock support mutable global variables?
The LazyLock
type is for global variables that are lazily initialized on first access. After that, the value will be immutable.
1 Like
You use interior mutability (atomic types, Mutex, RwLock) with LazyLock:
use std::sync::{LazyLock, Mutex};
static FOO: LazyLock<Mutex<String>> = LazyLock::new(Default::default);
fn main() {
dbg!(&*FOO.lock().unwrap());
*FOO.lock().unwrap() = "Hello".to_string();
dbg!(&*FOO.lock().unwrap());
}
2 Likes
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.