Struct std::collections::hash_set::HashSet

Overview

A 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(k1) == hash(k2)

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 std::collections::HashSet;

enum Tile {
   Wall,
}

let m = HashSet::new();

m.insert((0, 1));

assert!(m.contains((0, 1)));
assert!(!m.contains((0, 2)));

Methods

fn new() -> HashSet

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 std::collections::HashSet;

let set = HashSet::new();
fn with_capacity(capacity: u64) -> HashSet

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 std::collections::HashSet;

let set = HashSet::with_capacity(10);
assert!(set.capacity() >= 10);
fn len(self) -> u64

Returns the number of elements in the set.

Examples

use std::collections::HashSet;

let v = HashSet::new();
assert_eq!(v.len(), 0);
v.insert(1);
assert_eq!(v.len(), 1);
fn is_empty(self) -> bool

Returns true if the set contains no elements.

Examples

use std::collections::HashSet;

let v = HashSet::new();
assert!(v.is_empty());
v.insert(1);
assert!(!v.is_empty());
fn capacity(self) -> u64

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

Examples

use std::collections::HashSet;

let set = HashSet::with_capacity(100);
assert!(set.capacity() >= 100);
fn insert(self, key: any) -> bool

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 std::collections::HashSet;

let set = HashSet::new();

assert_eq!(set.insert(2), true);
assert_eq!(set.insert(2), false);
assert_eq!(set.len(), 1);
fn remove(self, key: any) -> bool

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

Examples

use std::collections::HashSet;

let set = HashSet::new();

set.insert(2);
assert_eq!(set.remove(2), true);
assert_eq!(set.remove(2), false);
fn contains(self, key: any) -> bool

Returns true if the set contains a value.

Examples

use std::collections::HashSet;

let set = HashSet::from_iter([1, 2, 3]);

assert!(set.contains(1));
assert!(!set.contains(4));
fn clear(self)

Clears the set, removing all values.

Examples

use std::collections::HashSet;

let v = HashSet::new();
v.insert(1);
v.clear();
assert!(v.is_empty());

Visits the values representing the difference, i.e., the values that are in self but not in other.

Examples

use std::collections::HashSet;

let a = HashSet::from_iter([1, 2, 3]);
let b = HashSet::from_iter([4, 2, 3, 4]);

let diff = a.difference(b).collect::<HashSet>();
assert_eq!(diff, [1].iter().collect::<HashSet>());

// Note that difference is not symmetric,
// and `b - a` means something else:
let diff = b.difference(a).collect::<HashSet>();
assert_eq!(diff, [4].iter().collect::<HashSet>());
fn extend(self, value: any)

Extend this set from an iterator.

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 std::collections::HashSet;

let a = HashSet::from_iter([1, 2, 3]);
let b = HashSet::from_iter([4, 2, 3, 4]);

let values = a.intersection(b).collect::<HashSet>();
assert_eq!(values, [2, 3].iter().collect::<HashSet>());
fn union(self, other: HashSet) -> Union

Visits the values representing the union, i.e., all the values in self or other, without duplicates.

Examples

use std::collections::HashSet;

let a = HashSet::from_iter([1, 2, 3]);
let b = HashSet::from_iter([4, 2, 3, 4]);

let union = a.union(b).collect::<HashSet>();
assert_eq!(union, HashSet::from_iter([1, 2, 3, 4]));

let union = b.union(a).collect::<HashSet>();
assert_eq!(union, HashSet::from_iter([1, 2, 3, 4]));
fn iter(self) -> Iter

Iterate over the hash set.

Examples

use std::collections::HashSet;

let set = HashSet::from_iter([3, 2, 1]);
let vec = set.iter().collect::<Vec>();
vec.sort();
assert_eq!(vec, [1, 2, 3]);
fn from_iter(it: any) -> HashSet

Convert a HashSet from an iterator.

The hashset can be converted from anything that implements the [INTO_ITER] protocol.

Examples

use std::collections::HashSet;

let set = HashSet::from_iter(["a", "b"]);
assert_eq!(set.len(), 2);
assert!(set.contains("a"));
assert!(set.contains("b"));

Trait Implementations

impl Clone for HashSet
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);
impl PartialEq for HashSet
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for HashSet

Protocols

protocol INTO_ITER
for item in value { }

Convert the set into an iterator.

Examples

use std::collections::HashSet;

let set = HashSet::from_iter([3, 2, 1]);
let vec = [];

for value in set {
   vec.push(value);
}

vec.sort();
assert_eq!(vec, [1, 2, 3]);
protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation to a string.

This calls the [DEBUG_FMT] protocol over all elements of the collection.

Examples

use std::collections::HashSet;

let set = HashSet::from_iter([1, 2, 3]);
println!("{:?}", set);
protocol CLONE
let $out = clone(value)

Clone a value.

protocol PARTIAL_EQ
if value == b { }

Perform a partial equality test between two sets.

Examples

Examples

use std::collections::HashSet;

let set = HashSet::from_iter([1, 2, 3]);
assert_eq!(set, HashSet::from_iter([1, 2, 3]));
assert_ne!(set, HashSet::from_iter([2, 3, 4]));
protocol EQ
if value == b { }

Perform a total equality test between two sets.

Examples

use std::ops::eq;
use std::collections::HashSet;

let set = HashSet::from_iter([1, 2, 3]);
assert!(eq(set, HashSet::from_iter([1, 2, 3])));
assert!(!eq(set, HashSet::from_iter([2, 3, 4])));