@@ -689,6 +689,30 @@ impl<K: Ord, V> BTreeMap<K, V> {
689689 } )
690690 }
691691
692+ /// Removes and returns the first element in the map.
693+ /// The key of this element is the minimum key that was in the map.
694+ ///
695+ /// # Examples
696+ ///
697+ /// Draining elements in ascending order, while keeping a usable map each iteration.
698+ ///
699+ /// ```
700+ /// #![feature(map_first_last)]
701+ /// use std::collections::BTreeMap;
702+ ///
703+ /// let mut map = BTreeMap::new();
704+ /// map.insert(1, "a");
705+ /// map.insert(2, "b");
706+ /// while let Some((key, _val)) = map.pop_first() {
707+ /// assert!(map.iter().all(|(k, _v)| *k > key));
708+ /// }
709+ /// assert!(map.is_empty());
710+ /// ```
711+ #[ unstable( feature = "map_first_last" , issue = "62924" ) ]
712+ pub fn pop_first ( & mut self ) -> Option < ( K , V ) > {
713+ self . first_entry ( ) . map ( |entry| entry. remove_entry ( ) )
714+ }
715+
692716 /// Returns the last key-value pair in the map.
693717 /// The key in this pair is the maximum key in the map.
694718 ///
@@ -742,6 +766,30 @@ impl<K: Ord, V> BTreeMap<K, V> {
742766 } )
743767 }
744768
769+ /// Removes and returns the last element in the map.
770+ /// The key of this element is the maximum key that was in the map.
771+ ///
772+ /// # Examples
773+ ///
774+ /// Draining elements in descending order, while keeping a usable map each iteration.
775+ ///
776+ /// ```
777+ /// #![feature(map_first_last)]
778+ /// use std::collections::BTreeMap;
779+ ///
780+ /// let mut map = BTreeMap::new();
781+ /// map.insert(1, "a");
782+ /// map.insert(2, "b");
783+ /// while let Some((key, _val)) = map.pop_last() {
784+ /// assert!(map.iter().all(|(k, _v)| *k < key));
785+ /// }
786+ /// assert!(map.is_empty());
787+ /// ```
788+ #[ unstable( feature = "map_first_last" , issue = "62924" ) ]
789+ pub fn pop_last ( & mut self ) -> Option < ( K , V ) > {
790+ self . last_entry ( ) . map ( |entry| entry. remove_entry ( ) )
791+ }
792+
745793 /// Returns `true` if the map contains a value for the specified key.
746794 ///
747795 /// The key may be any borrowed form of the map's key type, but the ordering
0 commit comments