Skip to content
CryoCryo home
Stdlibcollections

hashset

import std::collections::hashset; · source

Types

HashSet<T, A>

type struct HashSet<T, A = GlobalAlloc> {
    map: HashMap<T, (), A>;

    static new() -> HashSet<T>;
    static new_in(alloc: A) -> HashSet<T, A>;
    static with_capacity(capacity: u64) -> HashSet<T>;
    static with_seed(seed: u64) -> HashSet<T>;
    length(&this) -> u64;
    is_empty(&this) -> boolean;

    contains(&this, value: &T) -> boolean
    where T: Hash + Eq;

    insert(mut &this, value: T) -> boolean
    where T: Hash + Eq;

    try_insert(mut &this, value: T) -> Result<boolean, AllocError>
    where T: Hash + Eq;

    remove(mut &this, value: &T) -> boolean
    where T: Hash + Eq;

    clear(mut &this) -> void;

    iter(&this) -> implement Iterator<T>
    where T: Copy;

    iter_ref(&this) -> implement Iterator<T*>;
}

A thin wrapper over HashMap<T, ()> with the same storage, hasher, and growth behaviour, plus friendlier names. insert returns true when the value was newly added; remove returns true when it was present. iter() needs T: Copy; iter_ref() is the borrowing form for owning element types.

Properties

PropertyType
mapHashMap<T, (), A>

Constructors

static new() -> HashSet<T>;
static new_in(alloc: A) -> HashSet<T, A>;
static with_capacity(capacity: u64) -> HashSet<T>;
static with_seed(seed: u64) -> HashSet<T>;

Construct an empty set with an explicit, fixed hash seed instead of a random one (see HashMap::with_seed). For tests that need reproducible bucket placement / iteration order.

Methods

length(&this) -> u64;
is_empty(&this) -> boolean;
contains(&this, value: &T) -> boolean where T: Hash + Eq;

True if value is already a member.

insert(mut &this, value: T) -> boolean where T: Hash + Eq;

Insert value. Returns true if it wasn't already present, the common predicate for "this is a new one to look at." Panics on allocation failure; use try_insert to recover.

try_insert(mut &this, value: T) -> Result<boolean, AllocError> where T: Hash + Eq;
remove(mut &this, value: &T) -> boolean where T: Hash + Eq;

Remove value. Returns true if it was present.

clear(mut &this) -> void;
iter(&this) -> implement Iterator<T> where T: Copy;

Iterate over every element. Yields each value by bitcopy, so it is gated on T: Copy (mirrors HashMap::iter): a yielded owning value would alias set-owned storage and double-free on drop. Same mutation-invalidation contract as HashMap::iter.

iter_ref(&this) -> implement Iterator<T*>;

Iterate over every element by shared borrow, yielding T*. Unlike iter, this is sound for any T (owning or Copy) because it hands out borrows, never bitcopies - so it is the way to walk a HashSet<String> without cloning. Mirrors Array::iter_ref and HashMap::keys_ref. Same mutation-invalidation contract as iter.

Trait implementations

implement<T, A> trait Drop for struct HashSet<T, A> where A: Allocator
implement<T> trait Default for struct HashSet<T, GlobalAlloc>
implement<T> trait Clone for struct HashSet<T, GlobalAlloc> where T: Hash + Eq + Clone
implement<T, A> trait Display for struct HashSet<T, A> where T: Display, A: Allocator
std::fmt::display
implement<T, A> trait Debug for struct HashSet<T, A> where T: Debug, A: Allocator
std::fmt::display