pub struct RawVacantEntryMut<'a, K, V, S, A: Allocator = Global> { /* private fields */ }
Expand description

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

Examples

use core::hash::{BuildHasher, Hash};
use hashbrown::hash_map::{HashMap, RawEntryMut, RawVacantEntryMut};

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

fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
    use core::hash::Hasher;
    let mut state = hash_builder.build_hasher();
    key.hash(&mut state);
    state.finish()
}

let raw_v: RawVacantEntryMut<_, _, _> = match map.raw_entry_mut().from_key(&"a") {
    RawEntryMut::Vacant(view) => view,
    RawEntryMut::Occupied(_) => unreachable!(),
};
raw_v.insert("a", 10);
assert!(map[&"a"] == 10 && map.len() == 1);

// Nonexistent key (insert and update)
let hash = compute_hash(map.hasher(), &"b");
match map.raw_entry_mut().from_key_hashed_nocheck(hash, &"b") {
    RawEntryMut::Occupied(_) => unreachable!(),
    RawEntryMut::Vacant(view) => {
        let (k, value) = view.insert("b", 2);
        assert_eq!((*k, *value), ("b", 2));
        *value = 20;
    }
}
assert!(map[&"b"] == 20 && map.len() == 2);

let hash = compute_hash(map.hasher(), &"c");
match map.raw_entry_mut().from_hash(hash, |q| *q == "c") {
    RawEntryMut::Occupied(_) => unreachable!(),
    RawEntryMut::Vacant(view) => {
        assert_eq!(view.insert("c", 30), (&mut "c", &mut 30));
    }
}
assert!(map[&"c"] == 30 && map.len() == 3);

Implementations

Sets the value of the entry with the VacantEntry’s key, and returns a mutable reference to it.

Examples
use hashbrown::hash_map::{HashMap, RawEntryMut};

let mut map: HashMap<&str, u32> = [("a", 100), ("b", 200)].into();

match map.raw_entry_mut().from_key(&"c") {
    RawEntryMut::Occupied(_) => panic!(),
    RawEntryMut::Vacant(v) => assert_eq!(v.insert("c", 300), (&mut "c", &mut 300)),
}

assert_eq!(map[&"c"], 300);

Sets the value of the entry with the VacantEntry’s key, and returns a mutable reference to it.

Examples
use core::hash::{BuildHasher, Hash};
use hashbrown::hash_map::{HashMap, RawEntryMut};

fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
    use core::hash::Hasher;
    let mut state = hash_builder.build_hasher();
    key.hash(&mut state);
    state.finish()
}

let mut map: HashMap<&str, u32> = [("a", 100), ("b", 200)].into();
let key = "c";
let hash = compute_hash(map.hasher(), &key);

match map.raw_entry_mut().from_key_hashed_nocheck(hash, &key) {
    RawEntryMut::Occupied(_) => panic!(),
    RawEntryMut::Vacant(v) => assert_eq!(
        v.insert_hashed_nocheck(hash, key, 300),
        (&mut "c", &mut 300)
    ),
}

assert_eq!(map[&"c"], 300);

Set the value of an entry with a custom hasher function.

Examples
use core::hash::{BuildHasher, Hash};
use hashbrown::hash_map::{HashMap, RawEntryMut};

fn make_hasher<K, S>(hash_builder: &S) -> impl Fn(&K) -> u64 + '_
where
    K: Hash + ?Sized,
    S: BuildHasher,
{
    move |key: &K| {
        use core::hash::Hasher;
        let mut state = hash_builder.build_hasher();
        key.hash(&mut state);
        state.finish()
    }
}

let mut map: HashMap<&str, u32> = HashMap::new();
let key = "a";
let hash_builder = map.hasher().clone();
let hash = make_hasher(&hash_builder)(&key);

match map.raw_entry_mut().from_hash(hash, |q| q == &key) {
    RawEntryMut::Occupied(_) => panic!(),
    RawEntryMut::Vacant(v) => assert_eq!(
        v.insert_with_hasher(hash, key, 100, make_hasher(&hash_builder)),
        (&mut "a", &mut 100)
    ),
}
map.extend([("b", 200), ("c", 300), ("d", 400), ("e", 500), ("f", 600)]);
assert_eq!(map[&"a"], 100);

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.