@@ -663,19 +663,20 @@ impl<K: Ord, V> BTreeMap<K, V> {
663
663
///
664
664
/// # Examples
665
665
///
666
- /// Contrived way to `clear` a map:
667
- ///
668
666
/// ```
669
667
/// #![feature(map_first_last)]
670
668
/// use std::collections::BTreeMap;
671
669
///
672
670
/// let mut map = BTreeMap::new();
673
671
/// map.insert(1, "a");
674
672
/// map.insert(2, "b");
675
- /// while let Some(entry) = map.first_entry() {
676
- /// let (key, val) = entry.remove_entry();
677
- /// assert!(!map.contains_key(&key));
673
+ /// if let Some(mut entry) = map.first_entry() {
674
+ /// if *entry.key() > 0 {
675
+ /// entry.insert("first");
676
+ /// }
678
677
/// }
678
+ /// assert_eq!(*map.get(&1).unwrap(), "first");
679
+ /// assert_eq!(*map.get(&2).unwrap(), "b");
679
680
/// ```
680
681
#[ unstable( feature = "map_first_last" , issue = "62924" ) ]
681
682
pub fn first_entry ( & mut self ) -> Option < OccupiedEntry < ' _ , K , V > > {
@@ -715,19 +716,20 @@ impl<K: Ord, V> BTreeMap<K, V> {
715
716
///
716
717
/// # Examples
717
718
///
718
- /// Contrived way to `clear` a map:
719
- ///
720
719
/// ```
721
720
/// #![feature(map_first_last)]
722
721
/// use std::collections::BTreeMap;
723
722
///
724
723
/// let mut map = BTreeMap::new();
725
724
/// map.insert(1, "a");
726
725
/// map.insert(2, "b");
727
- /// while let Some(entry) = map.last_entry() {
728
- /// let (key, val) = entry.remove_entry();
729
- /// assert!(!map.contains_key(&key));
726
+ /// if let Some(mut entry) = map.last_entry() {
727
+ /// if *entry.key() > 0 {
728
+ /// entry.insert("last");
729
+ /// }
730
730
/// }
731
+ /// assert_eq!(*map.get(&1).unwrap(), "a");
732
+ /// assert_eq!(*map.get(&2).unwrap(), "last");
731
733
/// ```
732
734
#[ unstable( feature = "map_first_last" , issue = "62924" ) ]
733
735
pub fn last_entry ( & mut self ) -> Option < OccupiedEntry < ' _ , K , V > > {
0 commit comments