diff options
| author | bors <bors@rust-lang.org> | 2024-11-19 05:14:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-11-19 05:14:06 +0000 |
| commit | e6c1e14e5d487a90abeb5ba4dccc608521bbfb54 (patch) | |
| tree | b9781aa2b8d278e3261e7de668008705b5dd4299 /library/std/src | |
| parent | 5926e82dd1eae211c6e2ffe446de54df04798e89 (diff) | |
| parent | 56747f30b4d601d1d94c8dc32748e072bcb11573 (diff) | |
Auto merge of #133193 - fmease:rollup-v38ayvk, r=fmease
Rollup of 9 pull requests
Successful merges:
- #132758 (Improve `{BTreeMap,HashMap}::get_key_value` docs.)
- #133180 ([rustdoc] Fix items with generics not having their jump to def link generated)
- #133181 (Update books)
- #133182 (const_panic: inline in bootstrap builds to avoid f16/f128 crashes)
- #133185 (rustdoc-search: use smart binary search in bitmaps)
- #133186 (Document s390x-unknown-linux targets)
- #133187 (Add reference annotations for diagnostic attributes)
- #133191 (rustdoc book: Move `--test-builder(--wrapper)?` docs to unstable section.)
- #133192 (RELEASES.md: Don't document unstable `--test-build-wrapper`)
r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/collections/hash/map.rs | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index ded4f404d78..24bbc2f32cf 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -880,7 +880,11 @@ where self.base.get(k) } - /// Returns the key-value pair corresponding to the supplied key. + /// Returns the key-value pair corresponding to the supplied key. This is + /// potentially useful: + /// - for key types where non-identical keys can be considered equal; + /// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or + /// - for getting a reference to a key with the same lifetime as the collection. /// /// The supplied key may be any borrowed form of the map's key type, but /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for @@ -890,11 +894,39 @@ where /// /// ``` /// use std::collections::HashMap; + /// use std::hash::{Hash, Hasher}; + /// + /// #[derive(Clone, Copy, Debug)] + /// struct S { + /// id: u32, + /// # #[allow(unused)] // prevents a "field `name` is never read" error + /// name: &'static str, // ignored by equality and hashing operations + /// } + /// + /// impl PartialEq for S { + /// fn eq(&self, other: &S) -> bool { + /// self.id == other.id + /// } + /// } + /// + /// impl Eq for S {} + /// + /// impl Hash for S { + /// fn hash<H: Hasher>(&self, state: &mut H) { + /// self.id.hash(state); + /// } + /// } + /// + /// let j_a = S { id: 1, name: "Jessica" }; + /// let j_b = S { id: 1, name: "Jess" }; + /// let p = S { id: 2, name: "Paul" }; + /// assert_eq!(j_a, j_b); /// /// let mut map = HashMap::new(); - /// map.insert(1, "a"); - /// assert_eq!(map.get_key_value(&1), Some((&1, &"a"))); - /// assert_eq!(map.get_key_value(&2), None); + /// map.insert(j_a, "Paris"); + /// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris"))); + /// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case + /// assert_eq!(map.get_key_value(&p), None); /// ``` #[inline] #[stable(feature = "map_get_key_value", since = "1.40.0")] |
