Skip to main content

VacantEntry

Struct VacantEntry 

Source
pub struct VacantEntry<'a, K, V, S = BuildHasherDefault<AHasher>, A = Global>
where A: Allocator,
{ /* private fields */ }
Expand description

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

ยงExamples

use rune::alloc::hash_map::{Entry, HashMap, VacantEntry};

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

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

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