Skip to content

Commit 0df7f00

Browse files
committed
Addressed issues raised in rust-lang#44286.
This commit renames the `replace` function to `replace_entry`, and creates a seperate `replace_key` function for `OccupiedEntry`. The original `replace` function did not solve the use-case where the key needed to be replaced, but not the value. Documentation and naming has also been updated to better reflect what the original replace function does.
1 parent 69ee5a8 commit 0df7f00

File tree

1 file changed

+27
-5
lines changed
  • src/libstd/collections/hash

1 file changed

+27
-5
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,35 +2239,57 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22392239
self.key.take()
22402240
}
22412241

2242-
/// Replaces the entry, returning the old key and value.
2242+
/// Replaces the entry, returning the old key and value. The new key in the hash map will be
2243+
/// the key used to create this entry.
22432244
///
22442245
/// # Examples
22452246
///
22462247
/// ```
22472248
/// #![feature(map_entry_replace)]
2248-
/// use std::collections::HashMap;
2249-
/// use std::collections::hash_map::Entry;
2249+
/// use std::collections::hash_map::{Entry, HashMap};
22502250
///
22512251
/// let mut map: HashMap<String, u32> = HashMap::new();
22522252
/// map.insert("poneyland".to_string(), 15);
22532253
///
22542254
/// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
2255-
/// let (old_key, old_value): (String, u32) = entry.replace(16);
2255+
/// let (old_key, old_value): (String, u32) = entry.replace_entry(16);
22562256
/// assert_eq!(old_key, "poneyland");
22572257
/// assert_eq!(old_value, 15);
22582258
/// }
22592259
///
22602260
/// assert_eq!(map.get("poneyland"), Some(&16));
22612261
/// ```
22622262
#[unstable(feature = "map_entry_replace", issue = "44286")]
2263-
pub fn replace(mut self, value: V) -> (K, V) {
2263+
pub fn replace_entry(mut self, value: V) -> (K, V) {
22642264
let (old_key, old_value) = self.elem.read_mut();
22652265

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

22692269
(old_key, old_value)
22702270
}
2271+
2272+
/// Replaces the key in the hash map with the key used to create this entry.
2273+
///
2274+
/// # Examples
2275+
///
2276+
/// ```
2277+
/// #![feature(map_entry_replace)]
2278+
/// use std::collections::hash_map::{Entry, HashMap};
2279+
///
2280+
/// let mut map: HashMap<String, u32> = HashMap::new();
2281+
/// map.insert("poneyland".to_string(), 15);
2282+
///
2283+
/// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
2284+
/// let old_key = entry.replace_key();
2285+
/// assert_eq!(old_key, "poneyland");
2286+
/// }
2287+
/// ```
2288+
#[unstable(feature = "map_entry_replace", issue = "44286")]
2289+
pub fn replace_key(mut self) -> K {
2290+
let (old_key, _) = self.elem.read_mut();
2291+
mem::replace(old_key, self.key.unwrap())
2292+
}
22712293
}
22722294

22732295
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {

0 commit comments

Comments
 (0)