about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-01-09 01:58:52 +0800
committerkennytm <kennytm@gmail.com>2018-01-09 03:37:18 +0800
commit5ffaf4c291e179198045659a8554114064ab98ce (patch)
treea252b43649e8e84447506d11daf79ce12b551050 /src/libstd
parent6648dcd353ecad51886e30274c0f07dac4f41573 (diff)
parent1fc6ad56cd6ad4ea4231cd95af14ccd98f6a8960 (diff)
downloadrust-5ffaf4c291e179198045659a8554114064ab98ce.tar.gz
rust-5ffaf4c291e179198045659a8554114064ab98ce.zip
Rollup merge of #47259 - sfackler:map-remove-entry, r=dtolnay
Add HashMap::remove_entry

Implements #46344

r? @dtolnay
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/collections/hash/map.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index 7a79a472d58..4e5385c17e9 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -1241,6 +1241,46 @@ impl<K, V, S> HashMap<K, V, S>
         self.search_mut(k).into_occupied_bucket().map(|bucket| pop_internal(bucket).1)
     }
 
+    /// Removes a key from the map, returning the stored key and value if the
+    /// key was previously in the map.
+    ///
+    /// The key may be any borrowed form of the map's key type, but
+    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
+    /// the key type.
+    ///
+    /// [`Eq`]: ../../std/cmp/trait.Eq.html
+    /// [`Hash`]: ../../std/hash/trait.Hash.html
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(hash_map_remove_entry)]
+    /// use std::collections::HashMap;
+    ///
+    /// # fn main() {
+    /// let mut map = HashMap::new();
+    /// map.insert(1, "a");
+    /// assert_eq!(map.remove_entry(&1), Some((1, "a")));
+    /// assert_eq!(map.remove(&1), None);
+    /// # }
+    /// ```
+    #[unstable(feature = "hash_map_remove_entry", issue = "46344")]
+    pub fn remove_entry<Q: ?Sized>(&mut self, k: &Q) -> Option<(K, V)>
+        where K: Borrow<Q>,
+              Q: Hash + Eq
+    {
+        if self.table.size() == 0 {
+            return None;
+        }
+
+        self.search_mut(k)
+            .into_occupied_bucket()
+            .map(|bucket| {
+                let (k, v, _) = pop_internal(bucket);
+                (k, v)
+            })
+    }
+
     /// Retains only the elements specified by the predicate.
     ///
     /// In other words, remove all pairs `(k, v)` such that `f(&k,&mut v)` returns `false`.
@@ -3040,7 +3080,7 @@ mod test_map {
     }
 
     #[test]
-    fn test_pop() {
+    fn test_remove() {
         let mut m = HashMap::new();
         m.insert(1, 2);
         assert_eq!(m.remove(&1), Some(2));
@@ -3048,6 +3088,14 @@ mod test_map {
     }
 
     #[test]
+    fn test_remove_entry() {
+        let mut m = HashMap::new();
+        m.insert(1, 2);
+        assert_eq!(m.remove_entry(&1), Some((1, 2)));
+        assert_eq!(m.remove(&1), None);
+    }
+
+    #[test]
     fn test_iterate() {
         let mut m = HashMap::with_capacity(4);
         for i in 0..32 {