From c23ee767d94a8ea5e0aa782731f89f4e00fbdc7e Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Mon, 6 Apr 2020 19:03:18 +0200 Subject: [PATCH] BTreeMap first/last: make examples more to the point --- src/liballoc/collections/btree/map.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/liballoc/collections/btree/map.rs b/src/liballoc/collections/btree/map.rs index c782fb4f6a9..d550856b6ed 100644 --- a/src/liballoc/collections/btree/map.rs +++ b/src/liballoc/collections/btree/map.rs @@ -663,8 +663,6 @@ pub fn first_key_value(&self) -> Option<(&K, &V)> { /// /// # Examples /// - /// Contrived way to `clear` a map: - /// /// ``` /// #![feature(map_first_last)] /// use std::collections::BTreeMap; @@ -672,10 +670,13 @@ pub fn first_key_value(&self) -> Option<(&K, &V)> { /// let mut map = BTreeMap::new(); /// map.insert(1, "a"); /// map.insert(2, "b"); - /// while let Some(entry) = map.first_entry() { - /// let (key, val) = entry.remove_entry(); - /// assert!(!map.contains_key(&key)); + /// if let Some(mut entry) = map.first_entry() { + /// if *entry.key() > 0 { + /// entry.insert("first"); + /// } /// } + /// assert_eq!(*map.get(&1).unwrap(), "first"); + /// assert_eq!(*map.get(&2).unwrap(), "b"); /// ``` #[unstable(feature = "map_first_last", issue = "62924")] pub fn first_entry(&mut self) -> Option> { @@ -715,8 +716,6 @@ pub fn last_key_value(&self) -> Option<(&K, &V)> { /// /// # Examples /// - /// Contrived way to `clear` a map: - /// /// ``` /// #![feature(map_first_last)] /// use std::collections::BTreeMap; @@ -724,10 +723,13 @@ pub fn last_key_value(&self) -> Option<(&K, &V)> { /// let mut map = BTreeMap::new(); /// map.insert(1, "a"); /// map.insert(2, "b"); - /// while let Some(entry) = map.last_entry() { - /// let (key, val) = entry.remove_entry(); - /// assert!(!map.contains_key(&key)); + /// if let Some(mut entry) = map.last_entry() { + /// if *entry.key() > 0 { + /// entry.insert("last"); + /// } /// } + /// assert_eq!(*map.get(&1).unwrap(), "a"); + /// assert_eq!(*map.get(&2).unwrap(), "last"); /// ``` #[unstable(feature = "map_first_last", issue = "62924")] pub fn last_entry(&mut self) -> Option> {