Last active
September 6, 2024 23:39
-
-
Save gliheng/4c42adde081f11521b6513dbc30e995b to your computer and use it in GitHub Desktop.
Mutable lazy_static with a Mutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate lazy_static; | |
use std::sync::Mutex; | |
use std::collections::HashMap; | |
lazy_static! { | |
static ref HASHMAP: Mutex<HashMap<u32, String>> = Mutex::new({ | |
let mut m = HashMap::new(); | |
m.insert(0, String::from("foo")); | |
m.insert(1, String::from("bar")); | |
m.insert(2, String::from("baz")); | |
m | |
}); | |
} | |
fn main() { | |
let mut map = HASHMAP.lock().unwrap(); | |
map.insert(3, String::from("fuck")); | |
println!("There're {} entries in map\".", map.len()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment