std-method

BTreeMap::insert

Add or revise a record. If the key compares equal to a stored key, replace the value but retain the stored key. The previous value is returned if present. Expected stdout (not executed): None Some(3) 5 Common mistake: Expecting a duplicate key to create a second record.

Formal Syntax & Structure

rust syntaxrust
fn insert(&mut self, key: K, value: V) -> Option<V>

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeMap;    let mut stock = BTreeMap::new();    println!("{:?}", stock.insert("rice", 3));    println!("{:?}", stock.insert("rice", 5));    println!("{}", stock["rice"]);}
Rust Reference