std-method

BTreeMap::remove

Delete a record by key and recover only its value. Missing records produce None. Expected stdout (not executed): Some("reserved") None Common mistake: Assuming repeated removal returns the old value again.

Formal Syntax & Structure

rust syntaxrust
fn remove<Q>(&mut self, key: &Q) -> Option<V> where K: Borrow<Q> + Ord, Q: Ord + ?Sized

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeMap;    let mut seats = BTreeMap::from([(3, "reserved"), (8, "free")]);    println!("{:?}", seats.remove(&3));    println!("{:?}", seats.remove(&3));}
Rust Reference