trait

BuildHasher — create equivalent independent hashers

A builder creates fresh hashing states. Hashers created from the same builder must compute equal outputs for equivalent inputs. Separate builder instances may use different seeds. This supports independently hashing map keys. Expected stdout (not executed): true Common mistake: Reusing one accumulated hasher for all keys rather than building a fresh one.

Formal Syntax & Structure

rust syntaxrust
pub trait BuildHasher { type Hasher: Hasher; fn build_hasher(&self) -> Self::Hasher; /* provided methods omitted */ }

Working Example

Example Snippetrust
fn main() {    use std::hash::{BuildHasher, Hasher, RandomState};    let factory = RandomState::new();    let mut a = factory.build_hasher();    let mut b = factory.build_hasher();    a.write(b"note");    b.write(b"note");    println!("{}", a.finish() == b.finish());}
Rust Reference