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
| Property | Type |
|---|---|
map | HashMap<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>;
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;
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(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(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;
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*>;
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
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct HashSet<T, A = GlobalAlloc> {
map: HashMap<T, (), A>;
}HashSet<T, A>: unordered collection of unique values.
type parameter Ttype parameter Atype struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
HashSet.map: HashMap<T, (), A>type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmaptype parameter Tprimitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
type parameter Astatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static HashSet::new() -> HashSet<T>type struct HashSet<T, A = GlobalAlloc> {
map: HashMap<T, (), A>;
}HashSet<T, A>: unordered collection of unique values.
std::collections::hashsetstatic HashSet::new_in(alloc: A) -> HashSet<T, A>(parameter) alloc: Astatic HashSet::with_capacity(capacity: u64) -> HashSet<T>(parameter) capacity: u64static HashSet::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.
(parameter) seed: u64HashSet::length(&this) -> u64&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
HashSet::is_empty(&this) -> booleanHashSet::contains(&this, value: &T) -> boolean
where T: Hash + EqTrue if value is already a member.
(parameter) value: &Twhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
HashSet::insert(mut &this, value: T) -> boolean
where T: Hash + EqInsert 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.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
(parameter) value: THashSet::try_insert(mut &this, value: T) -> Result<boolean, AllocError>
where T: Hash + Eqtype struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}HashSet::remove(mut &this, value: &T) -> boolean
where T: Hash + EqRemove value. Returns true if it was present.
HashSet::clear(mut &this) -> voidHashSet::iter(&this) -> implement Iterator<T>
where T: CopyIterate 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.
-> implement Trait<T>An opaque type: some one concrete type that implements the trait, without naming it. The compiler infers the real type and uses it everywhere; callers see only the trait's methods. How the stdlib returns iterators without exposing their engines.
language referencetype trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markerHashSet::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.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::collections::hashset;HashSet<T, A>: unordered collection of unique values.
module pageHashSet::insert(mut &this, value: T) -> boolean
where T: Hash + EqInsert 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.
trueThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
HashSet::remove(mut &this, value: &T) -> boolean
where T: Hash + EqRemove value. Returns true if it was present.
HashSet::iter(&this) -> implement Iterator<T>
where T: CopyIterate 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.
HashSet::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.
HashSet.map: HashMap<T, (), A>static HashSet::new() -> HashSet<T>static HashSet::new_in(alloc: A) -> HashSet<T, A>static HashSet::with_capacity(capacity: u64) -> HashSet<T>static HashSet::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.
static HashMap::with_seed(seed: u64) -> HashMap<K, V, GlobalAlloc>Construct an empty map with an explicit, fixed hash seed instead of a random one. Intended for tests and any caller that needs reproducible bucket placement / iteration order; production code should prefer new so the seed stays unpredictable.
HashSet::length(&this) -> u64HashSet::is_empty(&this) -> booleanHashSet::contains(&this, value: &T) -> boolean
where T: Hash + EqTrue if value is already a member.
HashSet::try_insert(mut &this, value: T) -> Result<boolean, AllocError>
where T: Hash + EqHashSet::clear(mut &this) -> voidHashMap::iter(&this) -> implement Iterator<Pair<K, V>>
where K: Copy, V: CopyIterate over every entry, yielding Pair<K, V> (key first, value second). Iteration order is bucket-major and undefined across resizes; do not rely on it.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayArray::iter_ref(&this) -> implement Iterator<T*>Iterate by shared borrow, yielding T* for each element in order. Unlike iter, this is sound for every T (Copy or owning) because it hands out borrows, never copies. The pointers are invalidated by any reallocating method (push, reserve, insert, shrink_to_fit).
HashMap::keys_ref(&this) -> implement Iterator<K*>Iterate over the keys by shared borrow, yielding K*. Sound for any K (owning or Copy); this is how to walk the keys of a HashMap<String, V> - the common case - without the K: Copy gate on keys. Same borrow/invalidation contract as entries.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type parameter Awhere A: Allocator
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatortype trait Default {
static default() -> This;
}Default: a canonical "zero value" for a type.
std::core::defaulttype parameter Twhere T: Hash + Eq + Clone
type parameter Twhere T: Display
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype parameter Twhere T: Debug
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}