pub fn invert<K, V>(input: &HashMap<K, V>) -> HashMap<V, K>Expand description
Constructs a HashMap by inverting the keys and values of the input map.
This function iterates over each key-value pair in the input HashMap and
inserts them into a new HashMap with the keys and values swapped.
If duplicate values are present in the input map, the value from the last
Entry with that value will be used in the inverted map.
§Arguments
input- A reference to the inputHashMapto invert.
§Returns
HashMap<V, K>- A newHashMapwith keys and values inverted from the input.
§Examples
use lowdash::{invert, Entry};
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = invert(&map);
let mut expected = HashMap::new();
expected.insert(1, "a");
expected.insert(2, "b");
expected.insert(3, "c");
assert_eq!(result.len(), expected.len());
for (key, value) in &expected {
assert_eq!(result.get(key), Some(value));
}