| 1 |
//! This example shows how to wrap a data structure in a mutex to achieve safe mutability. |
| 2 |
extern crate lazy_static; |
| 3 |
use lazy_static::lazy_static; |
| 4 |
use std::collections::HashMap; |
| 5 |
use std::sync::Mutex; |
| 6 |
|
| 7 |
lazy_static! { |
| 8 |
static ref MUTEX_MAP: Mutex<HashMap<u32, &'static str>> = { |
| 9 |
let mut m = HashMap::new(); |
| 10 |
m.insert(0, "foo"); |
| 11 |
m.insert(1, "bar"); |
| 12 |
m.insert(2, "baz"); |
| 13 |
Mutex::new(m) |
| 14 |
}; |
| 15 |
} |
| 16 |
|
| 17 |
fn main() { |
| 18 |
MUTEX_MAP.lock().unwrap().insert(0, "boo"); |
| 19 |
println!( |
| 20 |
"The entry for `0` is \"{}\".", |
| 21 |
MUTEX_MAP.lock().unwrap().get(&0).unwrap() |
| 22 |
); |
| 23 |
} |