I\'d like to do the following:
Vec
for a certain key, and store it for later use.Ve
The entry API is designed for this. In manual form, it might look like
use std::collections::hash_map::Entry;
let values: &Vec = match map.entry(key) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(default)
};
Or one can use the briefer form:
map.entry(key).or_insert_with(|| default)
If default
is OK/cheap to compute even when it isn't inserted, it can also just be:
map.entry(key).or_insert(default)