From 1bc6c4b10e7036be6fe61999fabf3462eeed39be Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Fri, 6 Apr 2018 15:32:54 -0400 Subject: Clarify the difference between get_mut and into_mut for OccupiedEntry The examples for both hash_map::OccupiedEntry::get_mut and hash_map::OccupiedEntry::into_mut were almost identical. This led to some confusion over the difference, namely why you would ever use get_mut when into_mut gives alonger lifetime. Reddit thread: https://www.reddit.com/r/rust/comments/8a5swr/why_does_hashmaps This commit adds two lines and a comment to the example, to show that the entry object can be re-used after calling get_mut. --- src/libstd/collections/hash/map.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 935ea4b62b5..e733b0b8048 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2261,10 +2261,14 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// /// assert_eq!(map["poneyland"], 12); /// if let Entry::Occupied(mut o) = map.entry("poneyland") { - /// *o.get_mut() += 10; + /// *o.get_mut() += 10; + /// assert_eq!(o.get(), 22); + /// + /// // We can use the same Entry multiple times. + /// *o.get_mut() += 2; /// } /// - /// assert_eq!(map["poneyland"], 22); + /// assert_eq!(map["poneyland"], 24); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self) -> &mut V { -- cgit 1.4.1-3-g733a5 From ed1a8fff6282e1e9ed49e3638f365c358385fd99 Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Fri, 6 Apr 2018 17:10:35 -0400 Subject: Fixed typo --- src/libstd/collections/hash/map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index e733b0b8048..4a67c444ed6 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2262,7 +2262,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// assert_eq!(map["poneyland"], 12); /// if let Entry::Occupied(mut o) = map.entry("poneyland") { /// *o.get_mut() += 10; - /// assert_eq!(o.get(), 22); + /// assert_eq!(*o.get(), 22); /// /// // We can use the same Entry multiple times. /// *o.get_mut() += 2; -- cgit 1.4.1-3-g733a5 From a86f556ee37c13047f966c35effab8b334b7de67 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 2 Jun 2018 16:04:53 -0400 Subject: Add a couple lines describing differences between into_mut/get_mut. --- src/libstd/collections/hash/map.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 4a67c444ed6..5cbd8891364 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2250,6 +2250,11 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// Gets a mutable reference to the value in the entry. /// + /// If you need a reference to the `OccupiedEntry` which may outlive the + /// destruction of the `Entry` value, see [`into_mut`]. + /// + /// [`into_mut`]: #method.into_mut + /// /// # Examples /// /// ``` @@ -2278,6 +2283,10 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// Converts the OccupiedEntry into a mutable reference to the value in the entry /// with a lifetime bound to the map itself. /// + /// If you need multiple references to the `OccupiedEntry`, see [`get_mut`]. + /// + /// [`get_mut`]: #method.get_mut + /// /// # Examples /// /// ``` -- cgit 1.4.1-3-g733a5