0

Rust singleon - with Map

erikpa1 2 years ago updated by Alexander 2 years ago 0
Hi I found this page which is amazing, but Rust singleton with mutex is not complete, in rust itself there is big problem to create static object from Non-Const expression, this is solution how to solve it. Could be nice to add because Vec::new() is working, but its the most primitive case 99% use cases don't need.




use std::sync::Mutex;
use std::collections::HashSet;

static GLOBAL_DATA: Mutex<Option<HashSet<i32>>> = Mutex::new(None);

fn main() {
*GLOBAL_DATA.lock().unwrap() = Some(HashSet::from([42]));
println!("V2: {:?}", GLOBAL_DATA.lock().unwrap());
}