]> git.lizzy.rs Git - rust.git/commitdiff
Clarify the difference between get_mut and into_mut for OccupiedEntry
authorPhlosioneer <mattmdrr2@gmail.com>
Fri, 6 Apr 2018 19:32:54 +0000 (15:32 -0400)
committerCorey Farwell <coreyf@rwell.org>
Sat, 2 Jun 2018 19:51:39 +0000 (15:51 -0400)
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

index 935ea4b62b5eb932943e1e74f128c42d2bda7e18..e733b0b8048104351ff2a3a645ae9c5ad1303492 100644 (file)
@@ -2261,10 +2261,14 @@ pub fn get(&self) -> &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 {