std-method

BTreeSet::insert

Add an item if no equivalent item exists. The boolean tells whether the set gained a new element; the stored item is unchanged on a duplicate. Expected stdout (not executed): true false Common mistake: Interpreting false as an insertion failure due to allocation.

Formal Syntax & Structure

rust syntaxrust
fn insert(&mut self, value: T) -> bool

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeSet;    let mut ids = BTreeSet::new();    println!("{} {}", ids.insert(42), ids.insert(42));}
Rust Reference