Skip to content

Commit a689b2d

Browse files
Rollup merge of #51312 - frewsxcv:clarify-hash-map-entry-get-mut, r=dtolnay
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. Closes #49745
2 parents 5bbe1eb + dd88f88 commit a689b2d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/liballoc/btree/map.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,11 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
23692369

23702370
/// Gets a mutable reference to the value in the entry.
23712371
///
2372+
/// If you need a reference to the `OccupiedEntry` which may outlive the
2373+
/// destruction of the `Entry` value, see [`into_mut`].
2374+
///
2375+
/// [`into_mut`]: #method.into_mut
2376+
///
23722377
/// # Examples
23732378
///
23742379
/// ```
@@ -2380,9 +2385,13 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
23802385
///
23812386
/// assert_eq!(map["poneyland"], 12);
23822387
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
2383-
/// *o.get_mut() += 10;
2388+
/// *o.get_mut() += 10;
2389+
/// assert_eq!(*o.get(), 22);
2390+
///
2391+
/// // We can use the same Entry multiple times.
2392+
/// *o.get_mut() += 2;
23842393
/// }
2385-
/// assert_eq!(map["poneyland"], 22);
2394+
/// assert_eq!(map["poneyland"], 24);
23862395
/// ```
23872396
#[stable(feature = "rust1", since = "1.0.0")]
23882397
pub fn get_mut(&mut self) -> &mut V {
@@ -2391,6 +2400,10 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
23912400

23922401
/// Converts the entry into a mutable reference to its value.
23932402
///
2403+
/// If you need multiple references to the `OccupiedEntry`, see [`get_mut`].
2404+
///
2405+
/// [`get_mut`]: #method.get_mut
2406+
///
23942407
/// # Examples
23952408
///
23962409
/// ```

src/libstd/collections/hash/map.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,11 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22502250

22512251
/// Gets a mutable reference to the value in the entry.
22522252
///
2253+
/// If you need a reference to the `OccupiedEntry` which may outlive the
2254+
/// destruction of the `Entry` value, see [`into_mut`].
2255+
///
2256+
/// [`into_mut`]: #method.into_mut
2257+
///
22532258
/// # Examples
22542259
///
22552260
/// ```
@@ -2261,10 +2266,14 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22612266
///
22622267
/// assert_eq!(map["poneyland"], 12);
22632268
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
2264-
/// *o.get_mut() += 10;
2269+
/// *o.get_mut() += 10;
2270+
/// assert_eq!(*o.get(), 22);
2271+
///
2272+
/// // We can use the same Entry multiple times.
2273+
/// *o.get_mut() += 2;
22652274
/// }
22662275
///
2267-
/// assert_eq!(map["poneyland"], 22);
2276+
/// assert_eq!(map["poneyland"], 24);
22682277
/// ```
22692278
#[stable(feature = "rust1", since = "1.0.0")]
22702279
pub fn get_mut(&mut self) -> &mut V {
@@ -2274,6 +2283,10 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22742283
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
22752284
/// with a lifetime bound to the map itself.
22762285
///
2286+
/// If you need multiple references to the `OccupiedEntry`, see [`get_mut`].
2287+
///
2288+
/// [`get_mut`]: #method.get_mut
2289+
///
22772290
/// # Examples
22782291
///
22792292
/// ```

0 commit comments

Comments
 (0)