std-method
HashSet::intersection
Iterate over elements shared by both sets. The iterator borrows the sets; it does not allocate a new set by itself. Normalize order before printing multiple results. Expected stdout (not executed): [2, 3] Common mistake: Expecting the returned iterator to be an owned HashSet.
Formal Syntax & Structure
rust syntaxrust
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S>Working Example
Example Snippetrust
fn main() { use std::collections::HashSet; let a = HashSet::from([1, 2, 3]); let b = HashSet::from([2, 3, 4]); let mut common: Vec<_> = a.intersection(&b).copied().collect(); common.sort_unstable(); println!("{common:?}");}