Struct OccupiedEntry
pub struct OccupiedEntry<'a, K: 'a, V: 'a, A: Allocator = Global> { pub(in ::collections::hash::map) base: RustcOccupiedEntry<'a, K, V, A> }
A view into an occupied entry in a HashMap.
It is part of the Entry enum.
Fields
base: RustcOccupiedEntry<'a, K, V, A>
Implementations
impl<'a, K, V, A: Allocator> OccupiedEntry<'a, K, V, A>
fn key(&self) -> &KGets a reference to the key in the entry.
Examples
use std::collections::HashMap; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); assert_eq!(map.entry("poneyland").key(), &"poneyland");fn remove_entry(self) -> (K, V)Take the ownership of the key and value from the map.
Examples
use std::collections::HashMap; use std::collections::hash_map::Entry; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); if let Entry::Occupied(o) = map.entry("poneyland") { // We delete the entry from the map. o.remove_entry(); } assert_eq!(map.contains_key("poneyland"), false);fn get(&self) -> &VGets a reference to the value in the entry.
Examples
use std::collections::HashMap; use std::collections::hash_map::Entry; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); if let Entry::Occupied(o) = map.entry("poneyland") { assert_eq!(o.get(), &12); }fn get_mut(&mut self) -> &mut VGets a mutable reference to the value in the entry.
If you need a reference to the
OccupiedEntrywhich may outlive the destruction of theEntryvalue, seeinto_mut.Examples
use std::collections::HashMap; use std::collections::hash_map::Entry; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); assert_eq!(map["poneyland"], 12); if let Entry::Occupied(mut o) = map.entry("poneyland") { *o.get_mut() += 10; assert_eq!(*o.get(), 22); // We can use the same Entry multiple times. *o.get_mut() += 2; } assert_eq!(map["poneyland"], 24);fn into_mut(self) -> &'a mut VConverts the
OccupiedEntryinto a mutable reference to the value in the entry with a lifetime bound to the map itself.If you need multiple references to the
OccupiedEntry, seeget_mut.Examples
use std::collections::HashMap; use std::collections::hash_map::Entry; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); assert_eq!(map["poneyland"], 12); if let Entry::Occupied(o) = map.entry("poneyland") { *o.into_mut() += 10; } assert_eq!(map["poneyland"], 22);fn insert(&mut self, value: V) -> VSets the value of the entry, and returns the entry's old value.
Examples
use std::collections::HashMap; use std::collections::hash_map::Entry; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); if let Entry::Occupied(mut o) = map.entry("poneyland") { assert_eq!(o.insert(15), 12); } assert_eq!(map["poneyland"], 15);fn remove(self) -> VTakes the value out of the entry, and returns it.
Examples
use std::collections::HashMap; use std::collections::hash_map::Entry; let mut map: HashMap<&str, u32> = HashMap::new(); map.entry("poneyland").or_insert(12); if let Entry::Occupied(o) = map.entry("poneyland") { assert_eq!(o.remove(), 12); } assert_eq!(map.contains_key("poneyland"), false);
Trait Implementations
impl<K: Debug, V: Debug, A: Allocator> Debug for OccupiedEntry<'_, K, V, A>
fn fmt(&self, f: &mut Formatter<'_>) -> Result