@@ -2239,35 +2239,57 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
2239
2239
self . key . take ( )
2240
2240
}
2241
2241
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.
2243
2244
///
2244
2245
/// # Examples
2245
2246
///
2246
2247
/// ```
2247
2248
/// #![feature(map_entry_replace)]
2248
- /// use std::collections::HashMap;
2249
- /// use std::collections::hash_map::Entry;
2249
+ /// use std::collections::hash_map::{Entry, HashMap};
2250
2250
///
2251
2251
/// let mut map: HashMap<String, u32> = HashMap::new();
2252
2252
/// map.insert("poneyland".to_string(), 15);
2253
2253
///
2254
2254
/// 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);
2256
2256
/// assert_eq!(old_key, "poneyland");
2257
2257
/// assert_eq!(old_value, 15);
2258
2258
/// }
2259
2259
///
2260
2260
/// assert_eq!(map.get("poneyland"), Some(&16));
2261
2261
/// ```
2262
2262
#[ 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 ) {
2264
2264
let ( old_key, old_value) = self . elem . read_mut ( ) ;
2265
2265
2266
2266
let old_key = mem:: replace ( old_key, self . key . unwrap ( ) ) ;
2267
2267
let old_value = mem:: replace ( old_value, value) ;
2268
2268
2269
2269
( old_key, old_value)
2270
2270
}
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
+ }
2271
2293
}
2272
2294
2273
2295
impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
0 commit comments