On this page
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
- $O(1)$ average membership & set algebra: Supports
.union(),.intersection(),.difference(), and.symmetric_difference(). - Unordered: Use
BTreeSetwhen deterministic sorted iteration is needed.
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
- tuts/rust/std/collections/BTreeMap Linked here; links to this page; shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap Linked here; links to this page; shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections Linked here; links to this page; shared tags: #collections, #prog, #rust, #stdlib
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/std/collections/BinaryHeap shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/VecDeque shared tags: #collections, #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/Display shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/Error shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/error_and_fmt/format shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/ffi/CString shared tags: #prog, #rust, #stdlib, #tier