Trait std::hash::Hash1.0.0 [] [src]

pub trait Hash {
    fn hash<H>(&self, state: &mut H) where H: Hasher;

    fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher { ... }
}

A hashable type.

The H type parameter is an abstract hash state that is used by the Hash to compute the hash.

If you are also implementing Eq, there is an additional property that is important:

k1 == k2 -> hash(k1) == hash(k2)

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 derived, 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 Hash trait:

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);
    }
}

Required Methods

Feeds this value into the state given, updating the hasher as necessary.

Provided Methods

Feeds a slice of this type into the state provided.

Implementors