Struct HashSet

Source
pub struct HashSet<T: Hash + Eq, B: BuildHasher = RandomState> { /* private fields */ }
Expand description

A set of values that prevents duplicates with the help of the Hash trait.

Relies on HashMap internally, see documentation there for additional details.

A custom load factor is not supported at this point, with the default being 4/5.

It is a logic error for keys in a HashSet to be manipulated in a way that changes their hash. Because of this, HashSet’s API prevents mutable access to its keys.

§Time Complexity

See HashMap with the following additions.

Variables are defined as follows:

  • n: The number of items in the HashSet.
  • m: The number of items in the second HashSet.
MethodComplexity
difference*, -O(n)
-=O(m)
symmetric_difference*, ^O(n+m)
^=O(n+m)**, O(m)
intersection*, &O(n)
&=O(n)
union*, |O(n+m)
|=O(n+m)**, O(m)
is_subsetO(m)
is_supersetO(n)

In the event of a has collision, all methods will take additional time. This additional time is kept at a minimum and hash collisions are unlikely especially with a large capacity.

* When exhausted.

** If the HashMap already has capacity for the additional items, these methods will take O(m) instead.

Implementations§

Source§

impl<T: Hash + Eq, B: BuildHasher + Default> HashSet<T, B>

Source

pub fn new() -> HashSet<T, B>

Creates a new HashSet with capacity 0 and the default value for B. Memory will be allocated when the capacity changes.

Source

pub fn with_cap(cap: usize) -> HashSet<T, B>

Creates a new HashSet with the provided capacity, allowing insertions without reallocation. The default hasher will be used.

Source§

impl<T: Hash + Eq, B: BuildHasher> HashSet<T, B>

Source

pub fn with_hasher(hasher: B) -> HashSet<T, B>

Creates a new HashSet with capacity 0 and the provided hasher.

Source

pub fn with_cap_and_hasher(cap: usize, hasher: B) -> HashSet<T, B>

Creates a new HashSet with the provided capacity and hasher.

Source

pub const fn len(&self) -> usize

Returns the length of the HashSet (the number of elements it contains).

Source

pub const fn is_empty(&self) -> bool

Returns true if the HashSet contains no elements.

Source

pub const fn cap(&self) -> usize

Returns the current capacity of the HashSet.

Source

pub fn insert(&mut self, item: T) -> bool

Inserts the provided item into the HashSet, increasing its capacity if required. If the item was already included, no change is made and the method returns false. In other words, the method returns true if the insertion changes the HashSet.

Source

pub unsafe fn insert_unchecked(&mut self, item: T) -> bool

Inserts the provided item, without checking if the HashSet has enough capacity. If the item was already included, no change is made and the method returns false.

§Safety

It is the responsibility of the caller to ensure that the HashSet has enough capacity to add the provided item, using methods like reserve or with_cap.

§Panics

Panics if the HashSet has a capacity of 0, as it isn’t possible to find a bucket associated with the item.

Source

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

Returns true if the HashSet contains item.

Source

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

Returns a reference to the contained element equal to the provided item or None if there isn’t one.

Source

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

Removes item from the HashSet, returning it if it exists.

Source

pub fn reserve(&mut self, extra: usize)

Increases the capacity of the HashSet to ensure that len + extra elements will fit without exceeding the load factor.

Source

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

Returns an iterator over all elements in the HashSet, as references.

Trait Implementations§

Source§

impl<T: Hash + Eq + Clone, B: BuildHasher + Default> BitAnd for HashSet<T, B>

Source§

fn bitand(self, rhs: Self) -> Self::Output

Returns the intersection of self and rhs, as a HashSet. (self ∩ other)

Source§

type Output = HashSet<T, B>

The resulting type after applying the & operator.
Source§

impl<T: Hash + Eq, B: BuildHasher> BitAndAssign for HashSet<T, B>

Source§

fn bitand_assign(&mut self, rhs: Self)

Removes all items not in rhs from self to form an intersection in place.

Source§

impl<T: Hash + Eq + Clone, B: BuildHasher + Default> BitOr for HashSet<T, B>

Source§

fn bitor(self, rhs: Self) -> Self::Output

Returns the union of self and rhs, as a HashSet. (self ∪ other)

Source§

type Output = HashSet<T, B>

The resulting type after applying the | operator.
Source§

impl<T: Hash + Eq, B: BuildHasher> BitOrAssign for HashSet<T, B>

Source§

fn bitor_assign(&mut self, rhs: Self)

Adds all items from rhs to self to form a union in place.

Source§

impl<T: Hash + Eq + Clone, B: BuildHasher + Default> BitXor for HashSet<T, B>

Source§

fn bitxor(self, rhs: Self) -> Self::Output

Returns the symmetric difference of self and rhs, as a HashSet. (self △ other)

Source§

type Output = HashSet<T, B>

The resulting type after applying the ^ operator.
Source§

impl<T: Hash + Eq, B: BuildHasher> BitXorAssign for HashSet<T, B>

Source§

fn bitxor_assign(&mut self, rhs: Self)

Removes all items in both rhs and self from self to form the symmetric difference of the two in place.

Source§

impl<T: Hash + Eq + Debug, B: BuildHasher + Debug> Debug for HashSet<T, B>

Source§

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

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

impl<T: Hash + Eq, B: BuildHasher + Default> Default for HashSet<T, B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Hash + Eq + Debug, B: BuildHasher> Display for HashSet<T, B>

Source§

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

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

impl<T: Hash + Eq, B: BuildHasher> Extend<T> for HashSet<T, B>

Source§

fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, B, I> From<I> for HashSet<T, B>
where T: Hash + Eq, B: BuildHasher + Default, I: Iterator<Item = T> + ExactSizeIterator + TrustedLen,

Source§

fn from(value: I) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Eq, B: BuildHasher + Default> FromIterator<T> for HashSet<T, B>

Source§

fn from_iter<I: IntoIterator<Item = T>>(value: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: Hash + Eq, B: BuildHasher> IntoIterator for &'a HashSet<T, B>

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) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Hash + Eq, B: BuildHasher> IntoIterator for HashSet<T, B>

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§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Two HashSets are considered equal if they contain exactly the same elements.

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: Hash + Eq + Borrow<Q>, B: BuildHasher, Q: Hash + Eq + ?Sized> SetInterface<T, Q> for HashSet<T, B>

Source§

fn contains(&self, item: &Q) -> bool

Returns true if the set contains item.
Source§

fn get(&self, item: &Q) -> Option<&T>

Returns a reference to the contained element equal to the provided item or None if there isn’t one.
Source§

fn remove(&mut self, item: &Q) -> Option<T>

Removes item from the set, returning it if it exists.
Source§

impl<T: Hash + Eq, B: BuildHasher> SetIterator<T> for HashSet<T, B>

Source§

type Iter<'a> = Iter<'a, T> where Self: 'a

Source§

fn iter<'a>(&'a self) -> Self::Iter<'a>

Returns an iterator over all elements in the set, as references.
Source§

fn into_difference(self, other: Self) -> IntoDifference<Self, T>

Creates an owned iterator over all items that are in self but not other. (self \ other)
Source§

fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, Self, T>

Creates a borrowed iterator over all items that are in self but not other. (self \ other)
Source§

fn into_symmetric_difference( self, other: Self, ) -> IntoSymmetricDifference<Self, T>

Creates an owned iterator over all items that are in self or other but not both. (self △ other)
Source§

fn symmetric_difference<'a>( &'a self, other: &'a Self, ) -> SymmetricDifference<'a, Self, T>

Creates a borrowed iterator over all items that are in self or other but not both. (self △ other)
Source§

fn into_intersection(self, other: Self) -> IntoIntersection<Self, T>

Creates an owned iterator over all items that are in both self and other. (self ∩ other)
Source§

fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, Self, T>

Creates a borrowed iterator over all items that are in both self and other. (self ∩ other)
Source§

fn into_union(self, other: Self) -> IntoUnion<Self, T>

Creates an owned iterator over all items that are in either self or other. (self ∪ other)
Source§

fn union<'a>(&'a self, other: &'a Self) -> Union<'a, Self, T>

Creates a borrowed iterator over all items that are in either self or other. (self ∪ other)
Source§

fn is_subset(&self, other: &Self) -> bool

Returns true if other contains all elements of self. (self ⊆ other)
Source§

fn is_superset(&self, other: &Self) -> bool

Returns true if self contains all elements of other. (self ⊇ other)
Source§

impl<T: Hash + Eq + Clone, B: BuildHasher + Default> Sub for HashSet<T, B>

Source§

fn sub(self, rhs: Self) -> Self::Output

Returns the difference of self and rhs, as a HashSet. (self \ other)

Source§

type Output = HashSet<T, B>

The resulting type after applying the - operator.
Source§

impl<T: Hash + Eq, B: BuildHasher> SubAssign for HashSet<T, B>

Source§

fn sub_assign(&mut self, rhs: Self)

Removes all items in rhs from self to form the difference of the two in place.

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, B> UnwindSafe for HashSet<T, B>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.