Skip to content

Commit cc7698d

Browse files
author
Paul Kernfeld
committed
Add a mutex_map example
Fixes #128
1 parent 746a0f0 commit cc7698d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

examples/mutex_map.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)