]> git.lizzy.rs Git - rust.git/blobdiff - library/std/src/collections/hash/map.rs
Auto merge of #98284 - JohnTitor:rollup-7lbs143, r=JohnTitor
[rust.git] / library / std / src / collections / hash / map.rs
index d38fecc45b2fc93dfd21f7af09514c4419be1948..237c5ee73409eacd6c373aa0d03abfd5b9b864a4 100644 (file)
 /// // update a key, guarding against the key possibly not being set
 /// let stat = player_stats.entry("attack").or_insert(100);
 /// *stat += random_stat_buff();
+///
+/// // modify an entry before an insert with in-place mutation
+/// player_stats.entry("mana").and_modify(|mana| *mana += 200).or_insert(100);
 /// ```
 ///
 /// The easiest way to use `HashMap` with a custom key type is to derive [`Eq`] and [`Hash`].
@@ -585,7 +588,7 @@ pub fn is_empty(&self) -> bool {
     ///
     /// If the returned iterator is dropped before being fully consumed, it
     /// drops the remaining key-value pairs. The returned iterator keeps a
-    /// mutable borrow on the vector to optimize its implementation.
+    /// mutable borrow on the map to optimize its implementation.
     ///
     /// # Examples
     ///
@@ -829,8 +832,7 @@ pub fn shrink_to(&mut self, min_capacity: usize) {
     /// let mut letters = HashMap::new();
     ///
     /// for ch in "a short treatise on fungi".chars() {
-    ///     let counter = letters.entry(ch).or_insert(0);
-    ///     *counter += 1;
+    ///     letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
     /// }
     ///
     /// assert_eq!(letters[&'s'], 2);