std-method
BTreeMap::entry
Access a key through either a present or missing entry. The key is passed by ownership, and the handle holds the map borrow while used. Expected stdout (not executed): [("milk", 1), ("tea", 2)] Common mistake: Keeping an entry handle while trying to mutate the map separately.
Formal Syntax & Structure
rust syntaxrust
fn entry(&mut self, key: K) -> Entry<'_, K, V>Working Example
Example Snippetrust
fn main() { use std::collections::BTreeMap; let mut counts = BTreeMap::new(); for name in ["tea", "milk", "tea"] { *counts.entry(name).or_insert(0) += 1; } println!("{:?}", counts.into_iter().collect::<Vec<_>>());}