Skip to main content

VacantEntryRef

Struct VacantEntryRef 

Source
pub struct VacantEntryRef<'a, 'b, K, Q, V, S, A = Global>
where A: Allocator, Q: ?Sized,
{ /* private fields */ }
Expand description

A view into a vacant entry in a HashMap. It is part of the EntryRef enum.

ยงExamples

use rune::alloc::hash_map::{EntryRef, HashMap, VacantEntryRef};

let mut map = HashMap::<String, i32>::new();

let entry_v: VacantEntryRef<_, _, _, _> = match map.entry_ref("a") {
    EntryRef::Vacant(view) => view,
    EntryRef::Occupied(_) => unreachable!(),
};
entry_v.try_insert(10)?;
assert!(map["a"] == 10 && map.len() == 1);

// Nonexistent key (insert and update)
match map.entry_ref("b") {
    EntryRef::Occupied(_) => unreachable!(),
    EntryRef::Vacant(view) => {
        let value = view.try_insert(2)?;
        assert_eq!(*value, 2);
        *value = 20;
    }
}
assert!(map["b"] == 20 && map.len() == 2);