Skip to content

Commit c23ee76

Browse files
committed
BTreeMap first/last: make examples more to the point
1 parent bdbe56e commit c23ee76

File tree

1 file changed

+12
-10
lines changed
  • src/liballoc/collections/btree

1 file changed

+12
-10
lines changed

src/liballoc/collections/btree/map.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -663,19 +663,20 @@ impl<K: Ord, V> BTreeMap<K, V> {
663663
///
664664
/// # Examples
665665
///
666-
/// Contrived way to `clear` a map:
667-
///
668666
/// ```
669667
/// #![feature(map_first_last)]
670668
/// use std::collections::BTreeMap;
671669
///
672670
/// let mut map = BTreeMap::new();
673671
/// map.insert(1, "a");
674672
/// 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+
/// }
678677
/// }
678+
/// assert_eq!(*map.get(&1).unwrap(), "first");
679+
/// assert_eq!(*map.get(&2).unwrap(), "b");
679680
/// ```
680681
#[unstable(feature = "map_first_last", issue = "62924")]
681682
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {
@@ -715,19 +716,20 @@ impl<K: Ord, V> BTreeMap<K, V> {
715716
///
716717
/// # Examples
717718
///
718-
/// Contrived way to `clear` a map:
719-
///
720719
/// ```
721720
/// #![feature(map_first_last)]
722721
/// use std::collections::BTreeMap;
723722
///
724723
/// let mut map = BTreeMap::new();
725724
/// map.insert(1, "a");
726725
/// 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+
/// }
730730
/// }
731+
/// assert_eq!(*map.get(&1).unwrap(), "a");
732+
/// assert_eq!(*map.get(&2).unwrap(), "last");
731733
/// ```
732734
#[unstable(feature = "map_first_last", issue = "62924")]
733735
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> {

0 commit comments

Comments
 (0)