rustHow to join hashmaps in Rust
Joining two hashmaps in Rust can be done using the extend
method. This method takes an iterator of key-value pairs and adds them to the existing hashmap.
Code example:
let mut map1 = hashmap!{
"a" => 1,
"b" => 2,
};
let map2 = hashmap!{
"c" => 3,
"d" => 4,
};
map1.extend(map2);
Output
map1
now contains the key-value pairs {"a": 1, "b": 2, "c": 3, "d": 4}
Explanation:
let mut map1 = hashmap!{...}
: creates a mutable hashmapmap1
with the given key-value pairslet map2 = hashmap!{...}
: creates a hashmapmap2
with the given key-value pairsmap1.extend(map2)
: adds the key-value pairs frommap2
tomap1
Helpful links:
More of Rust
- How to use regex with bytes in Rust?
- How to replace a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to match a URL with a regex in Rust?
- How to replace strings using Rust regex?
- How to match the end of a line in a Rust regex?
- How to calculate the inverse of a matrix in Rust?
- How to get an entry from a HashSet in Rust?
- How to split a string by regex in Rust?
- How to implement PartialEq for a Rust HashMap?
See more codes...