Skip to content

Commit 0dce586

Browse files
committed
improved std::hash::Hasher docs
Part of #29357. * rephrased summary sentences to be less redundant * expanded top-level docs, adding a usage example and links to relevant methods (`finish`, `write` etc) as well as `Hash` * added examples to the `finish` and `write` methods
1 parent 37275b6 commit 0dce586

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

Diff for: src/libcore/hash/mod.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,73 @@ pub trait Hash {
208208
}
209209
}
210210

211-
/// A trait which represents the ability to hash an arbitrary stream of bytes.
211+
/// A trait for hashing an arbitrary stream of bytes.
212+
///
213+
/// Instances of `Hasher` usually represent state that is changed while hashing
214+
/// data.
215+
///
216+
/// `Hasher` provides a fairly basic interface for retrieving the generated hash
217+
/// (with [`finish`]), and writing integers as well as slices of bytes into an
218+
/// instance (with [`write`] and [`write_u8`] etc.). Most of the time, `Hasher`
219+
/// instances are used in conjunction with the [`Hash`] trait.
220+
///
221+
/// # Examples
222+
///
223+
/// ```
224+
/// use std::collections::hash_map::DefaultHasher;
225+
/// use std::hash::Hasher;
226+
///
227+
/// let mut hasher = DefaultHasher::new();
228+
///
229+
/// hasher.write_u32(1989);
230+
/// hasher.write_u8(11);
231+
/// hasher.write_u8(9);
232+
/// hasher.write(b"Huh?");
233+
///
234+
/// println!("Hash is {:x}!", hasher.finish());
235+
/// ```
236+
///
237+
/// [`Hash`]: trait.Hash.html
238+
/// [`finish`]: #tymethod.finish
239+
/// [`write`]: #tymethod.write
240+
/// [`write_u8`]: #method.write_u8
212241
#[stable(feature = "rust1", since = "1.0.0")]
213242
pub trait Hasher {
214243
/// Completes a round of hashing, producing the output hash generated.
244+
///
245+
/// # Examples
246+
///
247+
/// ```
248+
/// use std::collections::hash_map::DefaultHasher;
249+
/// use std::hash::Hasher;
250+
///
251+
/// let mut hasher = DefaultHasher::new();
252+
/// hasher.write(b"Cool!");
253+
///
254+
/// println!("Hash is {:x}!", hasher.finish());
255+
/// ```
215256
#[stable(feature = "rust1", since = "1.0.0")]
216257
fn finish(&self) -> u64;
217258

218259
/// Writes some data into this `Hasher`.
260+
///
261+
/// # Examples
262+
///
263+
/// ```
264+
/// use std::collections::hash_map::DefaultHasher;
265+
/// use std::hash::Hasher;
266+
///
267+
/// let mut hasher = DefaultHasher::new();
268+
/// let data = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
269+
///
270+
/// hasher.write(&data);
271+
///
272+
/// println!("Hash is {:x}!", hasher.finish());
273+
/// ```
219274
#[stable(feature = "rust1", since = "1.0.0")]
220275
fn write(&mut self, bytes: &[u8]);
221276

222-
/// Write a single `u8` into this hasher.
277+
/// Writes a single `u8` into this hasher.
223278
#[inline]
224279
#[stable(feature = "hasher_write", since = "1.3.0")]
225280
fn write_u8(&mut self, i: u8) {

0 commit comments

Comments
 (0)