Skip to content

Commit a4834c8

Browse files
authored
Rollup merge of rust-lang#98125 - KarlWithK:entry_add_modify_doc, r=Dylan-DPC
Entry and_modify doc This PR modifies the documentation for [HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html#) and [BTreeMap](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#) by introducing examples for `and_modify`. `and_modify` is a function that tends to give more idiomatic rust code when dealing with these data structures -- yet it lacked examples and was hidden away. This PR adds that and addresses rust-lang#98122. I've made some choices which I tried to explain in my commits. This is my first time contributing to rust, so hopefully, I made the right choices.
2 parents dc5d0d0 + 1040c90 commit a4834c8

File tree

2 files changed

+10
-3
lines changed
  • alloc/src/collections/btree
  • std/src/collections/hash

2 files changed

+10
-3
lines changed

alloc/src/collections/btree/map.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
161161
/// // update a key, guarding against the key possibly not being set
162162
/// let stat = player_stats.entry("attack").or_insert(100);
163163
/// *stat += random_stat_buff();
164+
///
165+
/// // modify an entry before an insert with in-place mutation
166+
/// player_stats.entry("mana").and_modify(|mana| *mana += 200).or_insert(100);
164167
/// ```
165168
#[stable(feature = "rust1", since = "1.0.0")]
166169
#[cfg_attr(not(test), rustc_diagnostic_item = "BTreeMap")]
@@ -1211,10 +1214,12 @@ impl<K, V, A: Allocator> BTreeMap<K, V, A> {
12111214
///
12121215
/// // count the number of occurrences of letters in the vec
12131216
/// for x in ["a", "b", "a", "c", "a", "b"] {
1214-
/// *count.entry(x).or_insert(0) += 1;
1217+
/// count.entry(x).and_modify(|curr| *curr += 1).or_insert(1);
12151218
/// }
12161219
///
12171220
/// assert_eq!(count["a"], 3);
1221+
/// assert_eq!(count["b"], 2);
1222+
/// assert_eq!(count["c"], 1);
12181223
/// ```
12191224
#[stable(feature = "rust1", since = "1.0.0")]
12201225
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, A>

std/src/collections/hash/map.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ use crate::sys;
164164
/// // update a key, guarding against the key possibly not being set
165165
/// let stat = player_stats.entry("attack").or_insert(100);
166166
/// *stat += random_stat_buff();
167+
///
168+
/// // modify an entry before an insert with in-place mutation
169+
/// player_stats.entry("mana").and_modify(|mana| *mana += 200).or_insert(100);
167170
/// ```
168171
///
169172
/// The easiest way to use `HashMap` with a custom key type is to derive [`Eq`] and [`Hash`].
@@ -829,8 +832,7 @@ where
829832
/// let mut letters = HashMap::new();
830833
///
831834
/// for ch in "a short treatise on fungi".chars() {
832-
/// let counter = letters.entry(ch).or_insert(0);
833-
/// *counter += 1;
835+
/// letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
834836
/// }
835837
///
836838
/// assert_eq!(letters[&'s'], 2);

0 commit comments

Comments
 (0)