Skip to content

Commit 0fb37fc

Browse files
committed
Improvided map_entry_replace examples
The current examples should be more realistic.
1 parent 0f8ee17 commit 0fb37fc

File tree

1 file changed

+22
-12
lines changed
  • src/libstd/collections/hash

1 file changed

+22
-12
lines changed

src/libstd/collections/hash/map.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -2247,17 +2247,18 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22472247
/// ```
22482248
/// #![feature(map_entry_replace)]
22492249
/// use std::collections::hash_map::{Entry, HashMap};
2250+
/// use std::rc::Rc;
22502251
///
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);
22532254
///
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);
22582260
/// }
22592261
///
2260-
/// assert_eq!(map.get("poneyland"), Some(&16));
22612262
/// ```
22622263
#[unstable(feature = "map_entry_replace", issue = "44286")]
22632264
pub fn replace_entry(mut self, value: V) -> (K, V) {
@@ -2276,13 +2277,22 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22762277
/// ```
22772278
/// #![feature(map_entry_replace)]
22782279
/// 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.
22792286
///
2280-
/// let mut map: HashMap<String, u32> = HashMap::new();
2281-
/// map.insert("poneyland".to_string(), 15);
2287+
/// reclaim_memory(&mut map, &known_strings);
22822288
///
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+
/// }
22862296
/// }
22872297
/// ```
22882298
#[unstable(feature = "map_entry_replace", issue = "44286")]

0 commit comments

Comments
 (0)