How to concatenate two HashMaps?

Suppose I have two hash maps:

let mut a : HashMap<u64, u32> = HashMap::new();
let mut b : HashMap<u64, u32> = HashMap::new();

How can I add the a hash map to the b? If they were vectors Vec, I would simply call Vec::append().

using .extend?

4 Likes

To get the union of a and b, try this:

a.into_iter().map(|(k, v)| b.insert(k, v));