std-method

HashSet::union

Iterate over elements present in either set, without repeating equivalent elements. For a new owned set, collect copied or cloned items as appropriate. Expected stdout (not executed): 3 true Common mistake: Expecting the union to mutate either input.

Formal Syntax & Structure

rust syntaxrust
pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S>

Working Example

Example Snippetrust
fn main() {    use std::collections::HashSet;    let a = HashSet::from([1, 2]);    let b = HashSet::from([2, 3]);    let all: HashSet<_> = a.union(&b).copied().collect();    println!("{} {}", all.len(), all.contains(&3));}
Rust Reference