std-method

HashMap::remove

Delete the matching entry and transfer the value to the caller. The stored key is discarded. None indicates that there was no matching entry. Expected stdout (not executed): Some("done") None Common mistake: Expecting removal to return a reference to a value that is no longer stored.

Formal Syntax & Structure

rust syntaxrust
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where K: Borrow<Q>, Q: Hash + Eq

Working Example

Example Snippetrust
fn main() {    use std::collections::HashMap;    let mut tasks = HashMap::from([("A", String::from("done"))]);    println!("{:?} {:?}", tasks.remove("A"), tasks.remove("A"));}
Rust Reference