about summary refs log tree commit diff
path: root/src/libstd/collections
diff options
context:
space:
mode:
authorJeroen Bollen <contact@jeroenbollen.eu>2017-11-11 16:21:49 +0100
committerJeroen Bollen <contact@jeroenbollen.eu>2017-11-11 19:45:32 +0100
commit0fb37fc67d0d0278022f355bed2aac0c2b772835 (patch)
tree1359bb526162ff1df984926d73fa9914ea86c99e /src/libstd/collections
parent0f8ee171b2d78acaee2fb3c7774af531510d945b (diff)
downloadrust-0fb37fc67d0d0278022f355bed2aac0c2b772835.tar.gz
rust-0fb37fc67d0d0278022f355bed2aac0c2b772835.zip
Improvided map_entry_replace examples
The current examples should be more realistic.
Diffstat (limited to 'src/libstd/collections')
-rw-r--r--src/libstd/collections/hash/map.rs34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index a7b02fb2b76..7a79a472d58 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -2247,17 +2247,18 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
     /// ```
     /// #![feature(map_entry_replace)]
     /// use std::collections::hash_map::{Entry, HashMap};
+    /// use std::rc::Rc;
     ///
-    /// let mut map: HashMap<String, u32> = HashMap::new();
-    /// map.insert("poneyland".to_string(), 15);
+    /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
+    /// map.insert(Rc::new("Stringthing".to_string()), 15);
     ///
-    /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
-    ///     let (old_key, old_value): (String, u32) = entry.replace_entry(16);
-    ///     assert_eq!(old_key, "poneyland");
-    ///     assert_eq!(old_value, 15);
+    /// let my_key = Rc::new("Stringthing".to_string());
+    ///
+    /// if let Entry::Occupied(entry) = map.entry(my_key) {
+    ///     // Also replace the key with a handle to our other key.
+    ///     let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
     /// }
     ///
-    /// assert_eq!(map.get("poneyland"), Some(&16));
     /// ```
     #[unstable(feature = "map_entry_replace", issue = "44286")]
     pub fn replace_entry(mut self, value: V) -> (K, V) {
@@ -2276,13 +2277,22 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
     /// ```
     /// #![feature(map_entry_replace)]
     /// use std::collections::hash_map::{Entry, HashMap};
+    /// use std::rc::Rc;
+    ///
+    /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
+    /// let mut known_strings: Vec<Rc<String>> = Vec::new();
+    ///
+    /// // Initialise known strings, run program, etc.
     ///
-    /// let mut map: HashMap<String, u32> = HashMap::new();
-    /// map.insert("poneyland".to_string(), 15);
+    /// reclaim_memory(&mut map, &known_strings);
     ///
-    /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
-    ///     let old_key = entry.replace_key();
-    ///     assert_eq!(old_key, "poneyland");
+    /// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
+    ///     for s in known_strings {
+    ///         if let Entry::Occupied(entry) = map.entry(s.clone()) {
+    ///             // Replaces the entry's key with our version of it in `known_strings`.
+    ///             entry.replace_key();
+    ///         }
+    ///     }
     /// }
     /// ```
     #[unstable(feature = "map_entry_replace", issue = "44286")]