about summary refs log tree commit diff
path: root/src/libstd/collections/hash
diff options
context:
space:
mode:
authorPhlosioneer <mattmdrr2@gmail.com>2018-04-06 15:32:54 -0400
committerCorey Farwell <coreyf@rwell.org>2018-06-02 15:51:39 -0400
commit1bc6c4b10e7036be6fe61999fabf3462eeed39be (patch)
tree1696cd63f6c7acdca92cb7cded87a7b195ef1d06 /src/libstd/collections/hash
parent4ecf12bf0eb8386626ccdb5f721a7183ccc4eba6 (diff)
downloadrust-1bc6c4b10e7036be6fe61999fabf3462eeed39be.tar.gz
rust-1bc6c4b10e7036be6fe61999fabf3462eeed39be.zip
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.
Diffstat (limited to 'src/libstd/collections/hash')
-rw-r--r--src/libstd/collections/hash/map.rs8
1 files changed, 6 insertions, 2 deletions
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 {