std-method
HashMap::remove_entry
Remove a record and recover ownership of both its original key and value. This differs from remove, which returns only the value. Expected stdout (not executed): Some(("tea", 2)) Common mistake: Assuming the returned key is the borrowed query rather than the stored key.
Formal Syntax & Structure
rust syntaxrust
pub fn remove_entry<Q: ?Sized>(&mut self, k: &Q) -> Option<(K, V)> where K: Borrow<Q>, Q: Hash + EqWorking Example
Example Snippetrust
fn main() { use std::collections::HashMap; let mut stock = HashMap::from([(String::from("tea"), 2)]); println!("{:?}", stock.remove_entry("tea"));}