Hash in std::hash - Rust (original) (raw)

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

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

Expand description

A hashable type.

Types implementing Hash are able to be hashed with an instance ofHasher.

§Implementing Hash

You can derive Hash with #[derive(Hash)] if all fields implement Hash. The resulting hash will be the combination of the values from callinghash on each field.

#[derive(Hash)]
struct Rustacean {
    name: String,
    country: String,
}

If you need more control over how a value is hashed, you can of course implement the Hash trait yourself:

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

§Hash and Eq

When implementing both Hash and Eq, it is important that the following property holds:

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

In other words, if two keys are equal, their hashes must also be equal.HashMap and HashSet both rely on this behavior.

Thankfully, you won’t need to worry about upholding this property when deriving both Eq and Hash with #[derive(PartialEq, Eq, Hash)].

Violating this property is a logic error. The behavior resulting from a logic error is not specified, but users of the trait must ensure that such logic errors do not result in undefined behavior. This means that unsafe code must not rely on the correctness of these methods.

§Prefix collisions

Implementations of hash should ensure that the data they pass to the Hasher are prefix-free. That is, values which are not equal should cause two different sequences of values to be written, and neither of the two sequences should be a prefix of the other.

For example, the standard implementation of Hash for &str passes an extra0xFF byte to the Hasher so that the values ("ab", "c") and ("a", "bc") hash differently.

§Portability

Due to differences in endianness and type sizes, data fed by Hash to a Hashershould not be considered portable across platforms. Additionally the data passed by most standard library types should not be considered stable between compiler versions.

This means tests shouldn’t probe hard-coded hash values or data fed to a Hasher and instead should check consistency with Eq.

Serialization formats intended to be portable between platforms or compiler versions should either avoid encoding hashes or only rely on Hash and Hasher implementations that provide additional guarantees.

1.0.0 · Source

Feeds this value into the given Hasher.

§Examples
use std::hash::{DefaultHasher, Hash, Hasher};

let mut hasher = DefaultHasher::new();
7920.hash(&mut hasher);
println!("Hash is {:x}!", hasher.finish());

1.3.0 · Source

Feeds a slice of this type into the given Hasher.

This method is meant as a convenience, but its implementation is also explicitly left unspecified. It isn’t guaranteed to be equivalent to repeated calls of hash and implementations ofHash should keep that in mind and call hash themselves if the slice isn’t treated as a whole unit in the PartialEqimplementation.

For example, a VecDeque implementation might naïvely callas_slices and then hash_slice on each slice, but this is wrong since the two slices can change with a call tomake_contiguous without affecting the PartialEqresult. Since these slices aren’t treated as singular units, and instead part of a larger deque, this method cannot be used.

§Examples
use std::hash::{DefaultHasher, Hash, Hasher};

let mut hasher = DefaultHasher::new();
let numbers = [6, 28, 496, 8128];
Hash::hash_slice(&numbers, &mut hasher);
println!("Hash is {:x}!", hasher.finish());

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Source§

1.0.0 · Source§

1.44.0 · Source§

1.0.0 · Source§

1.7.0 · Source§

Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.29.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.28.0 · Source§

1.0.0 · Source§

Source§

Source§

1.64.0 · Source§

1.64.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.1.0 · Source§

1.33.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Available on Unix only.

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

1.0.0 · Source§

1.19.0 · Source§

1.3.0 · Source§

1.8.0 · Source§

1.8.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Source§

Source§

1.10.0 · Source§

1.0.0 · Source§

1.55.0 · Source§

Source§

1.4.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.26.0 · Source§

1.0.0 · Source§

1.26.0 · Source§

Source§

Source§

Source§

1.0.0 · Source§

1.41.0 · Source§

1.17.0 · Source§

1.0.0 · Source§

1.36.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

This trait is implemented for tuples up to twelve items long.

1.19.0 · Source§

Source§

Source§

1.0.0 · Source§

Source§

1.21.0 · Source§

1.20.0 · Source§

1.28.0 · Source§

1.74.0 · Source§

1.0.0 · Source§

1.25.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

1.0.0 · Source§

1.0.0 · Source§

The hash of a vector is the same as that of the corresponding slice, as required by the core::borrow::Borrow implementation.

use std::hash::BuildHasher;

let b = std::hash::RandomState::new();
let v: Vec<u8> = vec![0xa8, 0x3c, 0x09];
let s: &[u8] = &[0xa8, 0x3c, 0x09];
assert_eq!(b.hash_one(v), b.hash_one(s));

1.0.0 · Source§

1.0.0 · Source§

The hash of an array is the same as that of the corresponding slice, as required by the Borrow implementation.

use std::hash::BuildHasher;

let b = std::hash::RandomState::new();
let a: [u8; 3] = [0xa8, 0x3c, 0x09];
let s: &[u8] = &[0xa8, 0x3c, 0x09];
assert_eq!(b.hash_one(a), b.hash_one(s));

Source§

Source§