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