-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Improve {BTreeMap,HashMap}::get_key_value
docs.
#132758
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you index a hashmap, you don't actually use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, let me explain how this PR came about. This method has one genuine use in the compiler, in So while I can see that the Also, I just looked through the version control history. The So now my inclination is to expand the text description of the methods to include the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds good to me. I agree that your use-case is probably the most useful one. |
||
/// 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")] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.