Skip to content

Commit

Permalink
Added a way to retrieve the key out of a HashMap when it's being repl…
Browse files Browse the repository at this point in the history
…aced.
  • Loading branch information
Binero committed Sep 2, 2017
1 parent 204c0a4 commit e9f01bc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,36 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
fn take_key(&mut self) -> Option<K> {
self.key.take()
}

/// Replaces the entry, returning the old key and value.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
/// use std::collections::hash_map::Entry;
///
/// let mut map: HashMap<String, u32> = HashMap::new();
/// map.insert(String::from("poneyland"), 15);
///
/// if let Entry::Occupied(entry) = map.entry(String::from("poneyland")) {
/// let (old_key, old_value): (String, u32) = entry.replace(16);
/// assert_eq!(old_key, "poneyland");
/// assert_eq!(old_value, 15);
/// }
///
/// assert_eq!(map.get("poneyland"), Some(&16));
///
/// ```
#[stable(feature = "rust1", since = "1.20.0")]
pub fn replace(mut self, value: V) -> (K, V) {
let (old_key, old_value) = self.elem.read_mut();

let old_key = mem::replace(old_key, self.key.unwrap());
let old_value = mem::replace(old_value, value);

(old_key, old_value)
}
}

impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
Expand Down

0 comments on commit e9f01bc

Please sign in to comment.