Skip to main content

OccupiedEntry

Struct OccupiedEntry 

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

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

ยงExamples

use rune::alloc::hash_map::{Entry, HashMap, OccupiedEntry};
use rune::alloc::prelude::*;

let mut map = HashMap::new();
map.try_extend([("a", 10), ("b", 20), ("c", 30)])?;

let _entry_o: OccupiedEntry<_, _, _> = map.entry("a").try_insert(100)?;
assert_eq!(map.len(), 3);

// Existing key (insert and update)
match map.entry("a") {
    Entry::Vacant(_) => unreachable!(),
    Entry::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("c") {
    Entry::Vacant(_) => unreachable!(),
    Entry::Occupied(view) => {
        assert_eq!(view.remove_entry(), ("c", 30));
    }
}
assert_eq!(map.get(&"c"), None);
assert_eq!(map.len(), 2);