about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-01-29 09:34:45 +0900
committerGitHub <noreply@github.com>2020-01-29 09:34:45 +0900
commitaf0c280be6744ec62a5eccd721de61837fa75345 (patch)
tree65eb4ef221f59b8dfc05cf72bbb296fa7a71768b
parentcb3884adaa538a9d380ac64dd3c9bb6ce6738da6 (diff)
parent5654305a7ba419e4a7c0334fc77d0ef41a1ca2ac (diff)
downloadrust-af0c280be6744ec62a5eccd721de61837fa75345.tar.gz
rust-af0c280be6744ec62a5eccd721de61837fa75345.zip
Rollup merge of #68378 - billyrieger:btreemap-remove-entry, r=KodrAus
Add BTreeMap::remove_entry

Implements https://github.com/rust-lang/rust/issues/66714.
-rw-r--r--src/liballoc/collections/btree/map.rs31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/liballoc/collections/btree/map.rs b/src/liballoc/collections/btree/map.rs
index e70f881bc3d..4dc004864fd 100644
--- a/src/liballoc/collections/btree/map.rs
+++ b/src/liballoc/collections/btree/map.rs
@@ -810,9 +810,38 @@ impl<K: Ord, V> BTreeMap<K, V> {
         K: Borrow<Q>,
         Q: Ord,
     {
+        self.remove_entry(key).map(|(_, v)| v)
+    }
+
+    /// 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 the ordering
+    /// on the borrowed form *must* match the ordering on the key type.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// #![feature(btreemap_remove_entry)]
+    /// use std::collections::BTreeMap;
+    ///
+    /// let mut map = BTreeMap::new();
+    /// map.insert(1, "a");
+    /// assert_eq!(map.remove_entry(&1), Some((1, "a")));
+    /// assert_eq!(map.remove_entry(&1), None);
+    /// ```
+    #[unstable(feature = "btreemap_remove_entry", issue = "66714")]
+    pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
+    where
+        K: Borrow<Q>,
+        Q: Ord,
+    {
         match search::search_tree(self.root.as_mut(), key) {
             Found(handle) => Some(
-                OccupiedEntry { handle, length: &mut self.length, _marker: PhantomData }.remove(),
+                OccupiedEntry { handle, length: &mut self.length, _marker: PhantomData }
+                    .remove_entry(),
             ),
             GoDown(_) => None,
         }