1use std::{collections::HashMap, hash::Hash};
2
3pub trait HashMapExt<K, V, I> {
4 fn concat(self, entries: I) -> Self;
5}
6
7impl<K, V, I> HashMapExt<K, V, I> for HashMap<K, V>
8where
9 K: Hash + Eq,
10 I: Iterator<Item = (K, V)>,
11{
12 fn concat(mut self, entries: I) -> Self {
13 for (key, value) in entries {
14 self.insert(key, value);
15 }
16
17 self
18 }
19}
20
21#[cfg(test)]
22mod tests {
23 use {super::HashMapExt, std::collections::HashMap};
24
25 #[test]
26 fn concat() {
27 let values: HashMap<&str, i64> = [("a", 10), ("b", 20)].into();
28 let new_items = [("c", 30), ("d", 40), ("e", 50)];
29
30 let actual = values.concat(new_items.into_iter());
31 let expected = [("a", 10), ("b", 20), ("c", 30), ("d", 40), ("e", 50)].into();
32
33 assert_eq!(actual, expected);
34 }
35}