@@ -208,18 +208,73 @@ pub trait Hash {
208
208
}
209
209
}
210
210
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
212
241
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
213
242
pub trait Hasher {
214
243
/// 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
+ /// ```
215
256
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
216
257
fn finish ( & self ) -> u64 ;
217
258
218
259
/// 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
+ /// ```
219
274
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
220
275
fn write ( & mut self , bytes : & [ u8 ] ) ;
221
276
222
- /// Write a single `u8` into this hasher.
277
+ /// Writes a single `u8` into this hasher.
223
278
#[ inline]
224
279
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
225
280
fn write_u8 ( & mut self , i : u8 ) {
0 commit comments