@@ -2239,35 +2239,67 @@ 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
+ /// use std::rc::Rc;
2251
+ ///
2252
+ /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
2253
+ /// map.insert(Rc::new("Stringthing".to_string()), 15);
2250
2254
///
2251
- /// let mut map: HashMap<String, u32> = HashMap::new();
2252
- /// map.insert("poneyland".to_string(), 15);
2255
+ /// let my_key = Rc::new("Stringthing".to_string());
2253
2256
///
2254
- /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
2255
- /// let (old_key, old_value): (String, u32) = entry.replace(16);
2256
- /// assert_eq!(old_key, "poneyland");
2257
- /// assert_eq!(old_value, 15);
2257
+ /// if let Entry::Occupied(entry) = map.entry(my_key) {
2258
+ /// // Also replace the key with a handle to our other key.
2259
+ /// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
2258
2260
/// }
2259
2261
///
2260
- /// assert_eq!(map.get("poneyland"), Some(&16));
2261
2262
/// ```
2262
2263
#[ unstable( feature = "map_entry_replace" , issue = "44286" ) ]
2263
- pub fn replace ( mut self , value : V ) -> ( K , V ) {
2264
+ pub fn replace_entry ( mut self , value : V ) -> ( K , V ) {
2264
2265
let ( old_key, old_value) = self . elem . read_mut ( ) ;
2265
2266
2266
2267
let old_key = mem:: replace ( old_key, self . key . unwrap ( ) ) ;
2267
2268
let old_value = mem:: replace ( old_value, value) ;
2268
2269
2269
2270
( old_key, old_value)
2270
2271
}
2272
+
2273
+ /// Replaces the key in the hash map with the key used to create this entry.
2274
+ ///
2275
+ /// # Examples
2276
+ ///
2277
+ /// ```
2278
+ /// #![feature(map_entry_replace)]
2279
+ /// use std::collections::hash_map::{Entry, HashMap};
2280
+ /// use std::rc::Rc;
2281
+ ///
2282
+ /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
2283
+ /// let mut known_strings: Vec<Rc<String>> = Vec::new();
2284
+ ///
2285
+ /// // Initialise known strings, run program, etc.
2286
+ ///
2287
+ /// reclaim_memory(&mut map, &known_strings);
2288
+ ///
2289
+ /// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
2290
+ /// for s in known_strings {
2291
+ /// if let Entry::Occupied(entry) = map.entry(s.clone()) {
2292
+ /// // Replaces the entry's key with our version of it in `known_strings`.
2293
+ /// entry.replace_key();
2294
+ /// }
2295
+ /// }
2296
+ /// }
2297
+ /// ```
2298
+ #[ unstable( feature = "map_entry_replace" , issue = "44286" ) ]
2299
+ pub fn replace_key ( mut self ) -> K {
2300
+ let ( old_key, _) = self . elem . read_mut ( ) ;
2301
+ mem:: replace ( old_key, self . key . unwrap ( ) )
2302
+ }
2271
2303
}
2272
2304
2273
2305
impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
0 commit comments