HashSet

Struct HashSet 

Source
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>

Source

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>

Source

pub fn from_std(hash_set: StdHashSet<T, S>) -> Self

Source

pub fn into_std(self) -> StdHashSet<T, S>

Source

pub fn capacity(&self) -> usize

Returns the number of elements the set can hold without reallocating.

Source

pub fn iter(&self) -> Iter<'_, T>

An iterator visiting all elements in arbitrary order. The iterator element type is &'a T.

Source

pub fn len(&self) -> usize

Returns the number of elements in the set.

Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no elements.

Source

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.

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&T) -> bool,

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.

Source

pub fn clear(&mut self)

Clears the set, removing all values.

Source

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.

Source

pub fn hasher(&self) -> &S

Returns a reference to the set’s BuildHasher.

Source§

impl<T, S> HashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source

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.

Source

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.

Source

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.

Source

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the set contains a value.

The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value type.

Source

pub fn get<Q>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value in the set, if any, that is equal to the given value.

The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value type.

Source

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.

Source

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.

Source

pub fn remove<Q>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes a value from the set. Returns whether the value was present in the set.

The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value type.

Source

pub fn take<Q>(&mut self, value: &Q) -> Option<T>
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes and returns the value in the set, if any, that is equal to the given one.

The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value type.

Trait Implementations§

Source§

impl<T, S> Debug for HashSet<T, S>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, S> Default for HashSet<T, S>
where S: Default,

Source§

fn default() -> HashSet<T, S>

Creates an empty HashSet<T, S> with the Default value for the hasher.

Source§

impl<'a, T, S> IntoIterator for &'a HashSet<T, S>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T, S> IntoIterator for HashSet<T, S>

Source§

fn into_iter(self) -> IntoIter<T>

Creates a consuming iterator, that is, one that moves each value out of the set in arbitrary order. The set cannot be used after calling this.

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

impl<T, S> PartialEq for HashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source§

fn eq(&self, other: &HashSet<T, S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, S> Eq for HashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Auto Trait Implementations§

§

impl<T, S> Freeze for HashSet<T, S>
where S: Freeze,

§

impl<T, S> RefUnwindSafe for HashSet<T, S>

§

impl<T, S> Send for HashSet<T, S>
where S: Send, T: Send,

§

impl<T, S> Sync for HashSet<T, S>
where S: Sync, T: Sync,

§

impl<T, S> Unpin for HashSet<T, S>
where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for HashSet<T, S>
where S: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.