1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
//! Trait for extending `HashMap` with `get_or_default`.
use std::collections::HashMap;
use std::hash::Hash;

pub(crate) trait MapWithDefault<K, V: Default> {
    fn get_or_default(&mut self, k: K) -> &mut V;
}

impl<K: Eq + Hash, V: Default> MapWithDefault<K, V> for HashMap<K, V> {
    fn get_or_default(&mut self, k: K) -> &mut V {
        self.entry(k).or_insert_with(V::default)
    }
}

#[test]
fn test_default() {
    let mut hash_map = HashMap::new();
    hash_map.insert(42, "hello");
    assert_eq!(*hash_map.get_or_default(43), "");
}