Skip to content

Instantly share code, notes, and snippets.

@gliheng
Last active August 19, 2023 02:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gliheng/4c42adde081f11521b6513dbc30e995b to your computer and use it in GitHub Desktop.
Save gliheng/4c42adde081f11521b6513dbc30e995b to your computer and use it in GitHub Desktop.
Mutable lazy_static with a Mutex
#[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