Skip to content

Commit 2cac9d7

Browse files
committed
clarify how insert() doesn't update keys
The first time I read the docs for `insert()`, I thought it was saying it didn't update existing *values*, and I was confused. Reword the docs to make it clear that `insert()` does update values.
1 parent 17d284b commit 2cac9d7

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/libcollections/btree/map.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
375375
///
376376
/// If the map did not have this key present, `None` is returned.
377377
///
378-
/// If the map did have this key present, the key is not updated, the
379-
/// value is updated and the old value is returned.
380-
/// See the [module-level documentation] for more.
378+
/// If the map did have this key present, the value is updated, and the old
379+
/// value is returned. The key is not updated, though; this matters for
380+
/// types that can be `==` without being identical. See the [module-level
381+
/// documentation] for more.
381382
///
382383
/// [module-level documentation]: index.html#insert-and-complex-keys
383384
///

src/libstd/collections/hash/map.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,10 @@ impl<K, V, S> HashMap<K, V, S>
11571157
///
11581158
/// If the map did not have this key present, `None` is returned.
11591159
///
1160-
/// If the map did have this key present, the key is not updated, the
1161-
/// value is updated and the old value is returned.
1162-
/// See the [module-level documentation] for more.
1160+
/// If the map did have this key present, the value is updated, and the old
1161+
/// value is returned. The key is not updated, though; this matters for
1162+
/// types that can be `==` without being identical. See the [module-level
1163+
/// documentation] for more.
11631164
///
11641165
/// [module-level documentation]: index.html#insert-and-complex-keys
11651166
///

src/libstd/collections/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,15 @@
397397
//! }
398398
//!
399399
//! let mut map = BTreeMap::new();
400-
//! map.insert(Foo { a: 1, b: "baz" }, ());
400+
//! map.insert(Foo { a: 1, b: "baz" }, 99);
401401
//!
402402
//! // We already have a Foo with an a of 1, so this will be updating the value.
403-
//! map.insert(Foo { a: 1, b: "xyz" }, ());
403+
//! map.insert(Foo { a: 1, b: "xyz" }, 100);
404404
//!
405-
//! // ... but the key hasn't changed. b is still "baz", not "xyz"
405+
//! // The value has been updated...
406+
//! assert_eq!(map.values().next().unwrap(), &100);
407+
//!
408+
//! // ...but the key hasn't changed. b is still "baz", not "xyz".
406409
//! assert_eq!(map.keys().next().unwrap().b, "baz");
407410
//! ```
408411

0 commit comments

Comments
 (0)