Skip to content

Commit 1bc6c4b

Browse files
Phlosioneerfrewsxcv
authored andcommitted
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.
1 parent 4ecf12b commit 1bc6c4b

File tree

1 file changed

+6
-2
lines changed
  • src/libstd/collections/hash

1 file changed

+6
-2
lines changed

src/libstd/collections/hash/map.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2261,10 +2261,14 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22612261
///
22622262
/// assert_eq!(map["poneyland"], 12);
22632263
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
2264-
/// *o.get_mut() += 10;
2264+
/// *o.get_mut() += 10;
2265+
/// assert_eq!(o.get(), 22);
2266+
///
2267+
/// // We can use the same Entry multiple times.
2268+
/// *o.get_mut() += 2;
22652269
/// }
22662270
///
2267-
/// assert_eq!(map["poneyland"], 22);
2271+
/// assert_eq!(map["poneyland"], 24);
22682272
/// ```
22692273
#[stable(feature = "rust1", since = "1.0.0")]
22702274
pub fn get_mut(&mut self) -> &mut V {

0 commit comments

Comments
 (0)