about summary refs log tree commit diff
path: root/src/libcore/hash
diff options
context:
space:
mode:
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>2016-05-22 18:29:13 -0400
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>2016-05-23 10:03:44 -0400
commit61bb9b2d0709647e2f8532f07c6aa0304cfb464e (patch)
treed4950b4dc06a4d33f89035e0b6abd57a402183c8 /src/libcore/hash
parentc41227fefc449b08f3535758bd75b4b19866e0f7 (diff)
downloadrust-61bb9b2d0709647e2f8532f07c6aa0304cfb464e.tar.gz
rust-61bb9b2d0709647e2f8532f07c6aa0304cfb464e.zip
Add more information about implementing `Hash`
A bit of duplication from the module documentation, but simplified
to be closer to being trivially copy-paste-able.
Diffstat (limited to 'src/libcore/hash')
-rw-r--r--src/libcore/hash/mod.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs
index 7f0d7517a57..844a24e80d1 100644
--- a/src/libcore/hash/mod.rs
+++ b/src/libcore/hash/mod.rs
@@ -97,9 +97,33 @@ mod sip;
 /// In other words, if two keys are equal, their hashes should also be equal.
 /// `HashMap` and `HashSet` both rely on this behavior.
 ///
+/// ## Derivable
+///
 /// This trait can be used with `#[derive]` if all fields implement `Hash`.
 /// When `derive`d, the resulting hash will be the combination of the values
 /// from calling `.hash()` on each field.
+///
+/// ## How can I implement `Hash`?
+///
+/// If you need more control over how a value is hashed, you need to implement
+/// the trait `Hash`:
+///
+/// ```
+/// use std::hash::{Hash, Hasher};
+///
+/// struct Person {
+///     id: u32,
+///     name: String,
+///     phone: u64,
+/// }
+///
+/// impl Hash for Person {
+///     fn hash<H: Hasher>(&self, state: &mut H) {
+///         self.id.hash(state);
+///         self.phone.hash(state);
+///     }
+/// }
+/// ```
 #[stable(feature = "rust1", since = "1.0.0")]
 pub trait Hash {
     /// Feeds this value into the state given, updating the hasher as necessary.