Skip to main content

Entry

Enum Entry 

Source
pub enum Entry<'a, T, S, A = Global>
where A: Allocator,
{ Occupied(OccupiedEntry<'a, T, S, A>), Vacant(VacantEntry<'a, T, S, A>), }
Expand description

A view into a single entry in a set, which may either be vacant or occupied.

This enum is constructed from the entry method on HashSet.

§Examples

use rune::alloc::{HashSet, Vec};
use rune::alloc::hash_set::{Entry, OccupiedEntry};
use rune::alloc::prelude::*;

let mut set = HashSet::new();
set.try_extend(["a", "b", "c"])?;
assert_eq!(set.len(), 3);

// Existing value (insert)
let entry: Entry<_, _> = set.entry("a");
let _raw_o: OccupiedEntry<_, _> = entry.try_insert()?;
assert_eq!(set.len(), 3);
// Nonexistent value (insert)
set.entry("d").try_insert()?;

// Existing value (or_try_insert)
set.entry("b").or_try_insert()?;
// Nonexistent value (or_try_insert)
set.entry("e").or_try_insert()?;

println!("Our HashSet: {:?}", set);

let mut vec: Vec<_> = set.iter().copied().try_collect()?;
// The `Iter` iterator produces items in arbitrary order, so the
// items must be sorted to test them against a sorted array.
vec.sort_unstable();
assert_eq!(vec, ["a", "b", "c", "d", "e"]);

Variants§

§

Occupied(OccupiedEntry<'a, T, S, A>)

An occupied entry.

§Examples

use rune::alloc::hash_set::Entry;
use rune::alloc::HashSet;

let mut set: HashSet<_> = HashSet::try_from(["a", "b"])?;

match set.entry("a") {
    Entry::Vacant(_) => unreachable!(),
    Entry::Occupied(_) => { }
}
§

Vacant(VacantEntry<'a, T, S, A>)

A vacant entry.

§Examples

use rune::alloc::hash_set::{Entry, HashSet};
let mut set = HashSet::new();

match set.entry("a") {
    Entry::Occupied(_) => unreachable!(),
    Entry::Vacant(_) => { }
}