summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authormchlrhw <4028654+mchlrhw@users.noreply.github.com>2017-09-20 16:02:10 +0100
committermchlrhw <4028654+mchlrhw@users.noreply.github.com>2017-10-06 09:10:31 +0100
commit9e36111fc60ff448bb8c2977dc51ccf0d3e3a3e9 (patch)
tree018cac1ec6b4e23d797799f83684e23cd8ff672c /src/libstd
parent183329cf738e8903058484845338f59a9e14d094 (diff)
downloadrust-9e36111fc60ff448bb8c2977dc51ccf0d3e3a3e9.tar.gz
rust-9e36111fc60ff448bb8c2977dc51ccf0d3e3a3e9.zip
Implement `entry_and_modify`
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/collections/hash/map.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index 96af2272578..73263d79d5f 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -2002,6 +2002,41 @@ impl<'a, K, V> Entry<'a, K, V> {
             Vacant(ref entry) => entry.key(),
         }
     }
+
+    /// Provides in-place mutable access to an occupied entry before any
+    /// potential inserts into the map.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(entry_and_modify)]
+    /// use std::collections::HashMap;
+    ///
+    /// let mut map: HashMap<&str, u32> = HashMap::new();
+    ///
+    /// map.entry("poneyland")
+    ///    .and_modify(|e| { *e += 1 })
+    ///    .or_insert(42);
+    /// assert_eq!(map["poneyland"], 42);
+    ///
+    /// map.entry("poneyland")
+    ///    .and_modify(|e| { *e += 1 })
+    ///    .or_insert(42);
+    /// assert_eq!(map["poneyland"], 43);
+    /// ```
+    #[unstable(feature = "entry_and_modify", issue = "44733")]
+    pub fn and_modify<F>(self, mut f: F) -> Self
+        where F: FnMut(&mut V)
+    {
+        match self {
+            Occupied(mut entry) => {
+                f(entry.get_mut());
+                Occupied(entry)
+            },
+            Vacant(entry) => Vacant(entry),
+        }
+    }
+
 }
 
 impl<'a, K, V: Default> Entry<'a, K, V> {