Skip to content

[docs] clarify how insert() doesn't update keys #31694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
///
/// If the map did not have this key present, `None` is returned.
///
/// If the map did have this key present, the key is not updated, the
/// value is updated and the old value is returned.
/// See the [module-level documentation] for more.
/// If the map did have this key present, the value is updated, and the old
/// value is returned. The key is not updated, though; this matters for
/// types that can be `==` without being identical. See the [module-level
/// documentation] for more.
///
/// [module-level documentation]: index.html#insert-and-complex-keys
///
Expand Down
7 changes: 4 additions & 3 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,10 @@ impl<K, V, S> HashMap<K, V, S>
///
/// If the map did not have this key present, `None` is returned.
///
/// If the map did have this key present, the key is not updated, the
/// value is updated and the old value is returned.
/// See the [module-level documentation] for more.
/// If the map did have this key present, the value is updated, and the old
/// value is returned. The key is not updated, though; this matters for
/// types that can be `==` without being identical. See the [module-level
/// documentation] for more.
///
/// [module-level documentation]: index.html#insert-and-complex-keys
///
Expand Down
9 changes: 6 additions & 3 deletions src/libstd/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,15 @@
//! }
//!
//! let mut map = BTreeMap::new();
//! map.insert(Foo { a: 1, b: "baz" }, ());
//! map.insert(Foo { a: 1, b: "baz" }, 99);
//!
//! // We already have a Foo with an a of 1, so this will be updating the value.
//! map.insert(Foo { a: 1, b: "xyz" }, ());
//! map.insert(Foo { a: 1, b: "xyz" }, 100);
//!
//! // ... but the key hasn't changed. b is still "baz", not "xyz"
//! // The value has been updated...
//! assert_eq!(map.values().next().unwrap(), &100);
//!
//! // ...but the key hasn't changed. b is still "baz", not "xyz".
//! assert_eq!(map.keys().next().unwrap().b, "baz");
//! ```

Expand Down