Struct std::collections::hash_set::HashSet
OverviewA hash set implemented as a HashMap
where the value is ()
.
As with the HashMap
type, a HashSet
requires that the elements implement the [EQ
] and [HASH
] protocols. If you implement these yourself, it is important that the following property holds:
k1 == k2 == hash
In other words, if two keys are equal, their hashes must be equal. Violating this property is a logic error.
It is also a logic error for a key to be modified in such a way that the key's hash, as determined by the [HASH
] protocol, or its equality, as determined by the [EQ
] protocol, changes while it is in the map. This is normally only possible through [Cell
], [RefCell
], global state, I/O, or unsafe code.
The behavior resulting from either logic error is not specified, but will be encapsulated to the HashSet
that observed the logic error and not result in undefined behavior. This could include panics, incorrect results, aborts, memory leaks, and non-termination.
Examples
use HashSet;
let m = new;
m.insert;
assert!;
assert!;
Methods
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.
Examples
use HashSet;
let set = new;
Creates an empty HashSet
with at least the specified capacity.
The hash set will be able to hold at least capacity
elements without reallocating. This method is allowed to allocate for more elements than capacity
. If capacity
is 0, the hash set will not allocate.
Examples
use HashSet;
let set = with_capacity;
assert!;
Returns the number of elements in the set.
Examples
use HashSet;
let v = new;
assert_eq!;
v.insert;
assert_eq!;
Returns true
if the set contains no elements.
Examples
use HashSet;
let v = new;
assert!;
v.insert;
assert!;
Returns the number of elements the set can hold without reallocating.
Examples
use HashSet;
let set = with_capacity;
assert!;
Adds a value to the set.
Returns whether the value was newly inserted. That is:
- If the set did not previously contain this value,
true
is returned. - If the set already contained this value,
false
is returned.
Examples
use HashSet;
let set = new;
assert_eq!;
assert_eq!;
assert_eq!;
Removes a value from the set. Returns whether the value was present in the set.
Examples
use HashSet;
let set = new;
set.insert;
assert_eq!;
assert_eq!;
Returns true
if the set contains a value.
Examples
use HashSet;
let set = from_iter;
assert!;
assert!;
Clears the set, removing all values.
Examples
use HashSet;
let v = new;
v.insert;
v.clear;
assert!;
Visits the values representing the difference, i.e., the values that are in self
but not in other
.
Examples
use HashSet;
let a = from_iter;
let b = from_iter;
let diff = a.difference .;
assert_eq!;
// Note that difference is not symmetric,
// and `b - a` means something else:
let diff = b.difference .;
assert_eq!;
Visits the values representing the intersection, i.e., the values that are both in self
and other
.
When an equal element is present in self
and other
then the resulting Intersection
may yield values to one or the other.
Examples
use HashSet;
let a = from_iter;
let b = from_iter;
let values = a.intersection .;
assert_eq!;
Visits the values representing the union, i.e., all the values in self
or other
, without duplicates.
Examples
use HashSet;
let a = from_iter;
let b = from_iter;
let union = a.union .;
assert_eq!;
let union = b.union .;
assert_eq!;
Iterate over the hash set.
Examples
use HashSet;
let set = from_iter;
let vec = set.iter .;
vec.sort;
assert_eq!;
Convert a HashSet
from an iterator.
The hashset can be converted from anything that implements the [INTO_ITER
] protocol.
Examples
use HashSet;
let set = from_iter;
assert_eq!;
assert!;
assert!;
Trait Implementations
Clone the specified value
.
Examples
let a = 42;
let b = a;
let c = a.clone;
a += 1;
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for inequality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Protocols
for item in value
Convert the set into an iterator.
Examples
use HashSet;
let set = from_iter;
let vec = ;
for value in set
vec.sort;
assert_eq!;
format!
Write a debug representation to a string.
This calls the [DEBUG_FMT
] protocol over all elements of the collection.
Examples
use HashSet;
let set = from_iter;
println!;
if value == b
Perform a partial equality test between two sets.
Examples
Examples
use HashSet;
let set = from_iter;
assert_eq!;
assert_ne!;
if value == b
Perform a total equality test between two sets.
Examples
use eq;
use HashSet;
let set = from_iter;
assert!;
assert!;