Struct ::std::collections::HashSet

Overview

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: i64) -> 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) -> i64

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) -> i64

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) -> 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) -> 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) -> bool

Returns true if the set contains a value.

Examples

use std::collections::HashSet;

let set = HashSet::from([1, 2, 3]);
assert_eq!(set.contains(1), true);
assert_eq!(set.contains(4), false);
fn clear(self) -> Tuple

Clears the set, removing all values.

Examples

use std::collections::HashSet;

let v = HashSet::new();
v.insert(1);
v.clear();
assert!(v.is_empty());
fn difference(self, other: HashSet) -> Iterator

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([1, 2, 3]);
let b = HashSet::from([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) -> Tuple

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([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);

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

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([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);

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

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

Iterate over the hash set.

Examples

use std::collections::HashSet;

let set = HashSet::from([3, 2, 1]);
let vec = set.iter().collect::<Vec>();
vec.sort();
assert_eq!(vec, [1, 2, 3]);
fn clone(self) -> HashSet
fn from(value) -> HashSet

Protocols

protocol into_iter
for item in value { }

Convert the set into an iterator.

Examples

use std::collections::HashSet;

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

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

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

Write a debug representation to a string.

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

Examples

use std::collections::HashSet;

let set = HashSet::from([1, 2, 3]);
println!("{:?}", set);
protocol partial_eq
if value == b { }

Perform a partial equality test between two sets.

Examples

Examples

use std::collections::HashSet;

let set = HashSet::from([1, 2, 3]);
assert_eq!(set, HashSet::from([1, 2, 3]));
assert_ne!(set, HashSet::from([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([1, 2, 3]);
assert!(eq(set, HashSet::from([1, 2, 3])));
assert!(!eq(set, HashSet::from([2, 3, 4])));