Skip to content

Commit 467e47f

Browse files
authored
Merge pull request #173 from paulkernfeld/mutex-map-example
Add a mutex_map example
2 parents 1ab3a27 + f4b70ba commit 467e47f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ matrix:
33
include:
44
- rust: 1.27.2
55
script:
6-
- cargo test --all-targets
6+
- cargo test --tests
77
- rust: stable
88
script:
99
- cargo test

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)