File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -97,9 +97,33 @@ mod sip;
97
97
/// In other words, if two keys are equal, their hashes should also be equal.
98
98
/// `HashMap` and `HashSet` both rely on this behavior.
99
99
///
100
+ /// ## Derivable
101
+ ///
100
102
/// This trait can be used with `#[derive]` if all fields implement `Hash`.
101
103
/// When `derive`d, the resulting hash will be the combination of the values
102
104
/// from calling `.hash()` on each field.
105
+ ///
106
+ /// ## How can I implement `Hash`?
107
+ ///
108
+ /// If you need more control over how a value is hashed, you need to implement
109
+ /// the trait `Hash`:
110
+ ///
111
+ /// ```
112
+ /// use std::hash::{Hash, Hasher};
113
+ ///
114
+ /// struct Person {
115
+ /// id: u32,
116
+ /// name: String,
117
+ /// phone: u64,
118
+ /// }
119
+ ///
120
+ /// impl Hash for Person {
121
+ /// fn hash<H: Hasher>(&self, state: &mut H) {
122
+ /// self.id.hash(state);
123
+ /// self.phone.hash(state);
124
+ /// }
125
+ /// }
126
+ /// ```
103
127
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
104
128
pub trait Hash {
105
129
/// Feeds this value into the state given, updating the hasher as necessary.
You can’t perform that action at this time.
0 commit comments