@@ -2247,17 +2247,18 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
2247
2247
/// ```
2248
2248
/// #![feature(map_entry_replace)]
2249
2249
/// use std::collections::hash_map::{Entry, HashMap};
2250
+ /// use std::rc::Rc;
2250
2251
///
2251
- /// let mut map: HashMap<String, u32> = HashMap::new();
2252
- /// map.insert("poneyland ".to_string(), 15);
2252
+ /// let mut map: HashMap<Rc< String> , u32> = HashMap::new();
2253
+ /// map.insert(Rc::new("Stringthing ".to_string() ), 15);
2253
2254
///
2254
- /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
2255
- /// let (old_key, old_value): (String, u32) = entry.replace_entry(16);
2256
- /// assert_eq!(old_key, "poneyland");
2257
- /// assert_eq!(old_value, 15);
2255
+ /// let my_key = Rc::new("Stringthing".to_string());
2256
+ ///
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
2264
pub fn replace_entry ( mut self , value : V ) -> ( K , V ) {
@@ -2276,13 +2277,22 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
2276
2277
/// ```
2277
2278
/// #![feature(map_entry_replace)]
2278
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.
2279
2286
///
2280
- /// let mut map: HashMap<String, u32> = HashMap::new();
2281
- /// map.insert("poneyland".to_string(), 15);
2287
+ /// reclaim_memory(&mut map, &known_strings);
2282
2288
///
2283
- /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
2284
- /// let old_key = entry.replace_key();
2285
- /// assert_eq!(old_key, "poneyland");
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
+ /// }
2286
2296
/// }
2287
2297
/// ```
2288
2298
#[ unstable( feature = "map_entry_replace" , issue = "44286" ) ]
0 commit comments