blob: 84ac9bbd0ddc4246c9ef4025f289540093997f95 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// run-pass
use std::collections::HashMap;
fn process_or_insert_default(map: &mut HashMap<usize, String>, key: usize) {
match map.get_mut(&key) {
Some(value) => {
process(value);
}
None => {
map.insert(key, "".to_string());
}
}
}
fn process(x: &str) {
assert_eq!(x, "Hello, world");
}
fn main() {
let map = &mut HashMap::new();
map.insert(22, format!("Hello, world"));
map.insert(44, format!("Goodbye, world"));
process_or_insert_default(map, 22);
process_or_insert_default(map, 66);
assert_eq!(map[&66], "");
}
|