Skip to content

Commit 5daab4a

Browse files
committed
Rollup merge of rust-lang#23814 - steveklabnik:gh23320, r=alexcrichton
Fixes rust-lang#23320
2 parents ef94b8a + f6c234f commit 5daab4a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/libcore/hash/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ mod sip;
7373
///
7474
/// The `H` type parameter is an abstract hash state that is used by the `Hash`
7575
/// to compute the hash.
76+
///
77+
/// If you are also implementing `Eq`, there is an additional property that
78+
/// is important:
79+
///
80+
/// ```text
81+
/// k1 == k2 -> hash(k1) == hash(k2)
82+
/// ```
83+
///
84+
/// In other words, if two keys are equal, their hashes should also be equal.
85+
/// `HashMap` and `HashSet` both rely on this behavior.
7686
#[stable(feature = "rust1", since = "1.0.0")]
7787
pub trait Hash {
7888
/// Feeds this value into the state given, updating the hasher as necessary.

src/libstd/collections/hash/map.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ fn test_resize_policy() {
214214
/// overridden with one of the constructors.
215215
///
216216
/// It is required that the keys implement the `Eq` and `Hash` traits, although
217-
/// this can frequently be achieved by using `#[derive(Eq, Hash)]`.
217+
/// this can frequently be achieved by using `#[derive(Eq, Hash)]`. If you
218+
/// implement these yourself, it is important that the following property holds:
219+
///
220+
/// ```text
221+
/// k1 == k2 -> hash(k1) == hash(k2)
222+
/// ```
223+
///
224+
/// In other words, if two keys are equal, their hashes must be equal.
218225
///
219226
/// It is a logic error for a key to be modified in such a way that the key's
220227
/// hash, as determined by the `Hash` trait, or its equality, as determined by

src/libstd/collections/hash/set.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ use super::state::HashState;
3434

3535
/// An implementation of a hash set using the underlying representation of a
3636
/// HashMap where the value is (). As with the `HashMap` type, a `HashSet`
37-
/// requires that the elements implement the `Eq` and `Hash` traits.
37+
/// requires that the elements implement the `Eq` and `Hash` traits. This can
38+
/// frequently be achieved by using `#[derive(Eq, Hash)]`. If you implement
39+
/// these yourself, it is important that the following property holds:
40+
///
41+
/// ```text
42+
/// k1 == k2 -> hash(k1) == hash(k2)
43+
/// ```
44+
///
45+
/// In other words, if two keys are equal, their hashes must be equal.
46+
///
3847
///
3948
/// It is a logic error for an item to be modified in such a way that the
4049
/// item's hash, as determined by the `Hash` trait, or its equality, as

0 commit comments

Comments
 (0)