pub struct OccupiedEntryRef<'a, 'b, K, Q, V, S, A = Global>{ /* private fields */ }Expand description
A view into an occupied entry in a HashMap.
It is part of the EntryRef enum.
ยงExamples
use rune::alloc::hash_map::{EntryRef, HashMap, OccupiedEntryRef};
use rune::alloc::prelude::*;
let mut map = HashMap::new();
map.try_extend([
("a".try_to_owned()?, 10),
("b".try_to_owned()?, 20),
("c".try_to_owned()?, 30)
])?;
let key = String::try_from("a")?;
let _entry_o: OccupiedEntryRef<_, _, _, _> = map.entry_ref(&key).try_insert(100)?;
assert_eq!(map.len(), 3);
// Existing key (insert and update)
match map.entry_ref("a") {
EntryRef::Vacant(_) => unreachable!(),
EntryRef::Occupied(mut view) => {
assert_eq!(view.get(), &100);
let v = view.get_mut();
*v *= 10;
assert_eq!(view.insert(1111), 1000);
}
}
assert_eq!(map["a"], 1111);
assert_eq!(map.len(), 3);
// Existing key (take)
match map.entry_ref("c") {
EntryRef::Vacant(_) => unreachable!(),
EntryRef::Occupied(view) => {
assert_eq!(view.remove_entry(), ("c".try_to_owned()?, 30));
}
}
assert_eq!(map.get("c"), None);
assert_eq!(map.len(), 2);