std-method

BTreeSet::union

Traverse members present in at least one set, without duplicating overlaps. Collect to own the resulting items if needed. Expected stdout (not executed): [1, 2, 3, 5, 6] Common mistake: Adding the two lengths to predict the union length despite overlaps.

Formal Syntax & Structure

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

Working Example

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