How to lookup from and insert into a HashMap in Rust?
The are multiple ways to check whether an element in a HashMap exist if not we insert it.1) Using The entry API
use std::collections::hash_map::Entry;
let values: &Vec<isize> = match map.entry(key) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(default)
};
2) Using a briefer form
map.entry(key).or_insert_with(|| default)