How to merge two HashMaps

Merge two HashMaps in Rust by calling the extend method on the target map with the source map as an argument.

Use the extend method to merge one HashMap into another, overwriting existing keys with values from the source map.

use std::collections::HashMap;

let mut map1 = HashMap::new();
map1.insert("a", 1);

let mut map2 = HashMap::new();
map2.insert("b", 2);
map2.insert("a", 10); // Overwrites map1's "a"

map1.extend(map2);
// map1 now contains: {"a": 10, "b": 2}

Note: extend consumes map2, so it cannot be used afterward.