On this page
  1. HashSet
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

HashSet

Prev: `HashMap` | Up: collections | Next: `BTreeMap`
Rel: collections, rust/builtins
Ref: Rust std — HashSet
Tags: #public #prog #rust #stdlib #collections #tier/core


Signature & Overview

HashSet<T>

Hash set backed by HashMap<T, ()> with $O(1)$ membership and .union() / .intersection().

Notes & Pitfalls

Example

use std::collections::HashSet;

fn main() {
    let a = HashSet::from([1, 2, 3]);
    let b = HashSet::from([2, 3, 4]);
    let shared: HashSet<_> = a.intersection(&b).copied().collect();
    assert_eq!(shared, HashSet::from([2, 3]));
}

Linked from

Related notes