Struct std::collections::hashset::HashSet
struct HashSet<K, H = DefaultHash> { ... }
H: Hasher<H>
A hash-based set collection.
HashSet<T>
is a wrapper around HashMap<T, ()>
. Elements must satisfy the
Hashable and Equatable protocols.
Example
use std::collections::HashSet;
let set = HashSet::new::<i32>();
defer set.free();
set.insert(1);
set.insert(2);
set.insert(3);
assert!(set.contains(&1));
assert!(set.contains(&2));
assert!(set.contains(&3));
Run this example
Methods
impl HashSet<K, H = DefaultHash> { ... }
H: Hasher<H>
-
fn new() -> HashSet<K, H>
Creates an empty set
Will not allocate until the first element is inserted.
-
Creates an empty set that can store up to
capacity
items without reallocating. -
Creates a set with a specific number of buckets.
As HashSet uses amortized growth strategy, prefer to use with_capacity unless you want to strictly control the size of the underlying array.
-
Reserves space for at least
additional
more elements to be inserted without reallocating.This follows the amortized growth strategy.
-
Resizes the underlying array to exactly the given
new_capacity
.Note that this could lead to poor performance if the resulting load factor is close to 1. Prefer to use reserve to grow the map and shrink_to_fit to shrink it.
Panics if the new capacity is lower than the current size.
Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); set.insert(1); set.insert(2); set.insert(3); set.rehash(1); // panics
Run this example -
fn shrink_to_fit(self: &mut HashSet<K, H>)
Reduces the size of underlying storage to fit the contained elements if needed.
-
Inserts a value into the set.
Returns
true
if the value was newly inserted andfalse
if the set already contained the value. -
Removes a value from the set.
Returns
true
if the value was present in the set,false
otherwise.Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); set.insert(1); assert!(!set.remove(&2)); assert!(set.remove(&1)); assert!(!set.remove(&1));
Run this example -
Retreives
true
if the set contains the given value,false
otherwise.Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); set.insert(1); assert!(set.contains(&1)); assert!(!set.contains(&2));
Run this example -
Retreives the number of elements in the set.
Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); set.insert(1); set.insert(2); set.insert(3); assert_eq!(set.len(), 3);
Run this example -
Returns
true
if the set is empty,false
otherwise.Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); assert!(set.is_empty()); set.insert(1); assert!(!set.is_empty());
Run this example -
fn clear(self: &mut HashSet<K, H>)
Clears the set, removing all values.
This does not reduce the capacity of the heap (use shrink_to_fit afterwards or use move instead).
Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); set.insert(1); set.insert(2); set.insert(3); set.clear(); assert!(set.is_empty());
Run this example -
fn iter(self: &HashSet<K, H>) -> HashSetIterator<K>
-
fn iter_ref(self: &HashSet<K, H>) -> HashSetRefIterator<K>
-
Removes the elements not mathing the given predicate.
Does not remove excess capacity, call shrink_to_fit afterwards, if this is desired.
Example
use std::collections::HashSet; let set = HashSet::new::<i32>(); defer set.free(); set.insert(1); set.insert(2); set.insert(3); set.retain(|x: &i32| -> bool { *x % 2 == 0 }); assert!(!set.contains(&1)); assert!(set.contains(&2)); assert!(!set.contains(&3));
Run this example -
fn free(self: &mut HashSet<K, H>)
Frees the memory backing the object.
-
fn free_all(self: &mut HashSet<K, H>)
Helper for collections that own heap-allocated objects. This is used to be able to do
defer col.free_all()
without much boilerplate.Example
use std::collections::HashSet; use std::string::StringBuf; use std::fmt::format; let v: HashSet<StringBuf> = HashSet::new(); defer v.free_all(); for i in 1..10 { v.insert("{} + {} = {}".format!(i, i, i + i).unwrap()); }
Run this example -
Returns a copy of the object.
impl HashSet<K, I, H = DefaultHash> { ... }
I: Iterator<I, K>
H: Hasher<H>
-
fn from_iter(iter: &mut I) -> HashSet<K, H>
Creates a hash map from an iterator of key-value pairs
Example
use std::collections::HashSet; let set: HashSet<i32> = HashSet::from_iter(&[1, 2, 3].iter()); defer set.free(); assert_eq!(set.len(), 3); assert!(set.contains(&1)); assert!(set.contains(&2)); assert!(set.contains(&3));
Run this example -
fn extend(self: &mut HashSet<K, H>, iter: &mut I)
Inserts additional elements into the set from an iterator of key-value pairs.
impl HashSet<B, K, H = DefaultHash> { ... }
K: Borrowable<K, B>
H: Hasher<H>
-
Checks if the value is present in the map by given borrowing of a key.
-
Removes the element from the set by given borrowing of a key.
If the element with the given key was present in the set, it is removed and returned. Otherwise,
Option::none()
is returned.