@@ -806,13 +806,42 @@ impl<K: Ord, V> BTreeMap<K, V> {
806
806
/// ```
807
807
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
808
808
pub fn remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
809
+ where
810
+ K : Borrow < Q > ,
811
+ Q : Ord ,
812
+ {
813
+ self . remove_entry ( key) . map ( |( _, v) | v)
814
+ }
815
+
816
+ /// Removes a key from the map, returning the stored key and value if the key
817
+ /// was previously in the map.
818
+ ///
819
+ /// The key may be any borrowed form of the map's key type, but the ordering
820
+ /// on the borrowed form *must* match the ordering on the key type.
821
+ ///
822
+ /// # Examples
823
+ ///
824
+ /// Basic usage:
825
+ ///
826
+ /// ```
827
+ /// #![feature(btreemap_remove_entry)]
828
+ /// use std::collections::BTreeMap;
829
+ ///
830
+ /// let mut map = BTreeMap::new();
831
+ /// map.insert(1, "a");
832
+ /// assert_eq!(map.remove_entry(&1), Some((1, "a")));
833
+ /// assert_eq!(map.remove_entry(&1), None);
834
+ /// ```
835
+ #[ unstable( feature = "btreemap_remove_entry" , issue = "66714" ) ]
836
+ pub fn remove_entry < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( K , V ) >
809
837
where
810
838
K : Borrow < Q > ,
811
839
Q : Ord ,
812
840
{
813
841
match search:: search_tree ( self . root . as_mut ( ) , key) {
814
842
Found ( handle) => Some (
815
- OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData } . remove ( ) ,
843
+ OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData }
844
+ . remove_entry ( ) ,
816
845
) ,
817
846
GoDown ( _) => None ,
818
847
}
0 commit comments