Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Collections

Rust HashSet union, intersection and difference

Reference for Rust HashSet set algebra: union, intersection, difference, symmetric_difference, is_subset, is_disjoint and the operator forms.

HashSet<T> has the operations you know from mathematics: union (in either set), intersection (in both), difference (in the first but not the second) and symmetric_difference (in exactly one). Each returns a lazy iterator of references; nothing is copied until you collect.

The operators |, &, - and ^ on &HashSet<T> produce new owned sets (the elements are cloned), which is handy when T: Clone.

Rust
let both: Vec<_> = a.intersection(&b).collect();
let merged: HashSet<_> = &a | &b;

Try it

Rust Four operations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Operators and relations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S>
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S>
pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S>
pub fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, S>) -> SymmetricDifference<'a, T, S>
pub fn is_subset(&self, other: &HashSet<T, S>) -> bool
pub fn is_superset(&self, other: &HashSet<T, S>) -> bool
pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool

// Operators, for T: Eq + Hash + Clone
impl BitOr<&HashSet<T, S>> for &HashSet<T, S>   // |  union
impl BitAnd<&HashSet<T, S>> for &HashSet<T, S>  // &  intersection
impl Sub<&HashSet<T, S>> for &HashSet<T, S>     // -  difference
impl BitXor<&HashSet<T, S>> for &HashSet<T, S>  // ^  symmetric difference
Name Description
other The second set; both sets must use the same element and hasher types.

Returns: iterators over &T for the four algebra methods, bool for the relation tests, and an owned HashSet<T, S> for the operators.

Errors and panics: none.

Usage notes

  • Lazy by default. a.union(&b) allocates nothing; .count(), .any() or a for loop can consume it directly.
  • Order is unspecified. The union yields all of a first and then the elements of b not in a, but the order inside each part is random. Sort before printing.
  • Cost: intersection and difference iterate over the smaller or first set and probe the other, so they are O(min(len)) or O(len of the first), not O(n * m).
  • Operators need &. a | b would move both sets; write &a | &b. The elements are cloned, so use the method form with .cloned().collect() only if you need custom control.
  • In-place variants do not exist for HashSet, but extend(other.iter().cloned()) does a union in place, and retain(|x| other.contains(x)) an intersection in place.
  • is_subset is reflexive: a set is a subset (and superset) of itself.
  • BTreeSet has the same methods and yields results in sorted order; see BTreeSet<T>.