pub struct HashSet<T, S = FxBuildHasher>(/* private fields */);Expand description
A hash set implemented as a HashMap where the value is ().
Implementations§
Source§impl<T> HashSet<T, FxBuildHasher>
impl<T> HashSet<T, FxBuildHasher>
Sourcepub fn new() -> HashSet<T, FxBuildHasher>
pub fn new() -> HashSet<T, FxBuildHasher>
Creates an empty HashSet.
The hash set is initially created with a capacity of 0, so it will not allocate until it is first inserted into.
Source§impl<T, S> HashSet<T, S>
impl<T, S> HashSet<T, S>
pub fn from_std(hash_set: StdHashSet<T, S>) -> Self
pub fn into_std(self) -> StdHashSet<T, S>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the set can hold without reallocating.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
An iterator visiting all elements in arbitrary order.
The iterator element type is &'a T.
Sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
If the returned iterator is dropped before being fully consumed, it drops the remaining elements. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all elements e for which f(&e) returns false.
The elements are visited in unsorted (and unspecified) order.
Sourcepub fn with_hasher(hasher: S) -> HashSet<T, S>
pub fn with_hasher(hasher: S) -> HashSet<T, S>
Creates a new empty hash set which will use the given hasher to hash keys.
The hash set is also created with the default initial capacity.
Warning: hasher is normally randomly generated, and
is designed to allow HashSets to be resistant to attacks that
cause many collisions and very poor performance. Setting it
manually using this function can expose a DoS attack vector.
The hash_builder passed should implement the BuildHasher trait for
the HashMap to be useful, see its documentation for details.
Sourcepub fn hasher(&self) -> &S
pub fn hasher(&self) -> &S
Returns a reference to the set’s BuildHasher.
Source§impl<T, S> HashSet<T, S>
impl<T, S> HashSet<T, S>
Sourcepub fn try_with_capacity(capacity: usize) -> Result<HashSet<T, S>, AllocError>where
S: Default,
pub fn try_with_capacity(capacity: usize) -> Result<HashSet<T, S>, AllocError>where
S: Default,
Creates an empty HashSet with the specified capacity.
The hash set will be able to hold at least capacity elements without
reallocating. If capacity is 0, the hash set will not allocate.
Sourcepub fn try_with_capacity_and_hasher(
capacity: usize,
hasher: S,
) -> Result<HashSet<T, S>, AllocError>
pub fn try_with_capacity_and_hasher( capacity: usize, hasher: S, ) -> Result<HashSet<T, S>, AllocError>
Creates an empty HashSet with the specified capacity, using
hasher to hash the keys.
The hash set will be able to hold at least capacity elements without
reallocating. If capacity is 0, the hash set will not allocate.
Warning: hasher is normally randomly generated, and
is designed to allow HashSets to be resistant to attacks that
cause many collisions and very poor performance. Setting it
manually using this function can expose a DoS attack vector.
The hash_builder passed should implement the BuildHasher trait for
the HashMap to be useful, see its documentation for details.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError>
Tries to reserve capacity for at least additional more elements to be inserted
in the given HashSet<K, V>. The collection may reserve more space to avoid
frequent reallocations.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn try_insert(&mut self, value: T) -> Result<bool, AllocError>
pub fn try_insert(&mut self, value: T) -> Result<bool, AllocError>
Adds a value to the set.
If the set did not have this value present, true is returned.
If the set did have this value present, false is returned.
Sourcepub fn replace(&mut self, value: T) -> Result<Option<T>, AllocError>
pub fn replace(&mut self, value: T) -> Result<Option<T>, AllocError>
Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.