Skip to content

Commit af0c280

Browse files
authored
Rollup merge of #68378 - billyrieger:btreemap-remove-entry, r=KodrAus
Add BTreeMap::remove_entry Implements #66714.
2 parents cb3884a + 5654305 commit af0c280

File tree

1 file changed

+30
-1
lines changed
  • src/liballoc/collections/btree

1 file changed

+30
-1
lines changed

src/liballoc/collections/btree/map.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,42 @@ impl<K: Ord, V> BTreeMap<K, V> {
806806
/// ```
807807
#[stable(feature = "rust1", since = "1.0.0")]
808808
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)>
809837
where
810838
K: Borrow<Q>,
811839
Q: Ord,
812840
{
813841
match search::search_tree(self.root.as_mut(), key) {
814842
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(),
816845
),
817846
GoDown(_) => None,
818847
}

0 commit comments

Comments
 (0)