File tree 3 files changed +28
-2
lines changed
3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,16 @@ mod sip;
73
73
///
74
74
/// The `H` type parameter is an abstract hash state that is used by the `Hash`
75
75
/// 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.
76
86
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
77
87
pub trait Hash {
78
88
/// Feeds this value into the state given, updating the hasher as necessary.
Original file line number Diff line number Diff line change @@ -214,7 +214,14 @@ fn test_resize_policy() {
214
214
/// overridden with one of the constructors.
215
215
///
216
216
/// 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.
218
225
///
219
226
/// It is a logic error for a key to be modified in such a way that the key's
220
227
/// hash, as determined by the `Hash` trait, or its equality, as determined by
Original file line number Diff line number Diff line change @@ -34,7 +34,16 @@ use super::state::HashState;
34
34
35
35
/// An implementation of a hash set using the underlying representation of a
36
36
/// 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
+ ///
38
47
///
39
48
/// It is a logic error for an item to be modified in such a way that the
40
49
/// item's hash, as determined by the `Hash` trait, or its equality, as
You can’t perform that action at this time.
0 commit comments