Expand description
可散列的类型。
实现 Hash
的类型可以通过 Hasher
的实例进行 hash
化。
如果所有字段都要实现 Hash
,则可以用 #[derive(Hash)]
派生 Hash
。
产生的哈希将是在每个字段上调用 hash
的值的组合。
#[derive(Hash)]
struct Rustacean {
name: String,
country: String,
}
Run
如果您需要对值的散列方式进行更多控制,则当然可以自己实现 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);
}
}
Run
同时实现 Hash
和 Eq
时,保持以下属性很重要:
k1 == k2 -> hash(k1) == hash(k2)
换句话说,如果两个键相等,则它们的哈希也必须相等。
HashMap
和 HashSet
都依赖此行为。
值得庆幸的是,在使用 #[derive(PartialEq, Eq, Hash)]
派生 Eq
和 Hash
时,您不必担心维护此属性。
将该值输入给定的 Hasher
。
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
7920.hash(&mut hasher);
println!("Hash is {:x}!", hasher.finish());
Run
将这种类型的切片送入给定的 Hasher
中。
此方法是为了方便起见,但它的实现也明确未指定。
它不能保证等同于 hash
的重复调用,并且 Hash
的实现应该记住这一点,如果在 PartialEq
实现中没有将 6 视为整个单元,则调用 hash
本身。
例如,一个 VecDeque
实现可能天真地调用 as_slices
然后 hash_slice
对每个调用 hash_slice
,但这是错误的,因为两个切片可以随调用更改为 make_contiguous
而不会影响 PartialEq
结果。
由于这些切片不被视为单一单元,而是更大双端队列的一部分,因此无法使用此方法。
use std::collections::hash_map::DefaultHasher;
use std::hash::{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());
Run
impl<A, B, C, D> Hash for (A, B, C, D) where
C: Hash,
B: Hash,
A: Hash,
D: Hash + ?Sized,
[src]
impl<A, B, C, D, E> Hash for (A, B, C, D, E) where
C: Hash,
E: Hash + ?Sized,
B: Hash,
A: Hash,
D: Hash,
[src]
impl<A, B, C, D, E, F> Hash for (A, B, C, D, E, F) where
C: Hash,
F: Hash + ?Sized,
E: Hash,
B: Hash,
A: Hash,
D: Hash,
[src]
impl<A, B, C, D, E, F, G> Hash for (A, B, C, D, E, F, G) where
C: Hash,
F: Hash,
E: Hash,
B: Hash,
A: Hash,
G: Hash + ?Sized,
D: Hash,
[src]
impl<A, B, C, D, E, F, G, H> Hash for (A, B, C, D, E, F, G, H) where
C: Hash,
F: Hash,
E: Hash,
H: Hash + ?Sized,
B: Hash,
A: Hash,
G: Hash,
D: Hash,
[src]
impl<A, B, C, D, E, F, G, H, I> Hash for (A, B, C, D, E, F, G, H, I) where
C: Hash,
F: Hash,
I: Hash + ?Sized,
E: Hash,
H: Hash,
B: Hash,
A: Hash,
G: Hash,
D: Hash,
[src]
impl<A, B, C, D, E, F, G, H, I, J> Hash for (A, B, C, D, E, F, G, H, I, J) where
C: Hash,
F: Hash,
I: Hash,
E: Hash,
H: Hash,
B: Hash,
A: Hash,
J: Hash + ?Sized,
G: Hash,
D: Hash,
[src]
impl<A, B, C, D, E, F, G, H, I, J, K> Hash for (A, B, C, D, E, F, G, H, I, J, K) where
C: Hash,
F: Hash,
K: Hash + ?Sized,
I: Hash,
E: Hash,
H: Hash,
B: Hash,
A: Hash,
J: Hash,
G: Hash,
D: Hash,
[src]
impl<A, B, C, D, E, F, G, H, I, J, K, L> Hash for (A, B, C, D, E, F, G, H, I, J, K, L) where
C: Hash,
F: Hash,
K: Hash,
I: Hash,
E: Hash,
H: Hash,
B: Hash,
A: Hash,
J: Hash,
G: Hash,
D: Hash,
L: Hash + ?Sized,
[src]
impl<Ret> Hash for unsafe extern "C" fn() -> Ret
1.4.0[src]
impl<Ret, A> Hash for extern "C" fn(A) -> Ret
1.4.0[src]
impl<Ret, A> Hash for extern "C" fn(A, ...) -> Ret
1.4.0[src]
impl<Ret, A> Hash for unsafe extern "C" fn(A) -> Ret
1.4.0[src]
impl<Ret, A> Hash for unsafe extern "C" fn(A, ...) -> Ret
1.4.0[src]
impl<Ret, A, B> Hash for extern "C" fn(A, B) -> Ret
1.4.0[src]
impl<Ret, A, B> Hash for extern "C" fn(A, B, ...) -> Ret
1.4.0[src]
impl<Ret, A, B> Hash for unsafe fn(A, B) -> Ret
1.4.0[src]
impl<Ret, A, B> Hash for unsafe extern "C" fn(A, B) -> Ret
1.4.0[src]
impl<Ret, A, B> Hash for unsafe extern "C" fn(A, B, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C> Hash for fn(A, B, C) -> Ret
1.4.0[src]
impl<Ret, A, B, C> Hash for extern "C" fn(A, B, C) -> Ret
1.4.0[src]
impl<Ret, A, B, C> Hash for extern "C" fn(A, B, C, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C> Hash for unsafe fn(A, B, C) -> Ret
1.4.0[src]
impl<Ret, A, B, C> Hash for unsafe extern "C" fn(A, B, C) -> Ret
1.4.0[src]
impl<Ret, A, B, C> Hash for unsafe extern "C" fn(A, B, C, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D> Hash for fn(A, B, C, D) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D> Hash for extern "C" fn(A, B, C, D) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D> Hash for extern "C" fn(A, B, C, D, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D> Hash for unsafe fn(A, B, C, D) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D> Hash for unsafe extern "C" fn(A, B, C, D) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D> Hash for unsafe extern "C" fn(A, B, C, D, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Hash for fn(A, B, C, D, E) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Hash for extern "C" fn(A, B, C, D, E) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Hash for extern "C" fn(A, B, C, D, E, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Hash for unsafe fn(A, B, C, D, E) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Hash for unsafe extern "C" fn(A, B, C, D, E) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Hash for unsafe extern "C" fn(A, B, C, D, E, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Hash for fn(A, B, C, D, E, F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Hash for extern "C" fn(A, B, C, D, E, F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Hash for extern "C" fn(A, B, C, D, E, F, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Hash for unsafe fn(A, B, C, D, E, F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Hash for unsafe extern "C" fn(A, B, C, D, E, F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Hash for unsafe extern "C" fn(A, B, C, D, E, F, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Hash for fn(A, B, C, D, E, F, G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Hash for extern "C" fn(A, B, C, D, E, F, G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Hash for extern "C" fn(A, B, C, D, E, F, G, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Hash for unsafe fn(A, B, C, D, E, F, G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Hash for fn(A, B, C, D, E, F, G, H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Hash for extern "C" fn(A, B, C, D, E, F, G, H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Hash for extern "C" fn(A, B, C, D, E, F, G, H, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Hash for unsafe fn(A, B, C, D, E, F, G, H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Hash for fn(A, B, C, D, E, F, G, H, I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Hash for unsafe fn(A, B, C, D, E, F, G, H, I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for fn(A, B, C, D, E, F, G, H, I, J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, J, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for unsafe extern "C" fn(A, B, C, D, E, F, G, H, I, J, K, L, ...) -> Ret
1.4.0[src]
根据 core::borrow::Borrow
实现的要求,vector 的哈希值与相应的 3 的哈希值相同。
#![feature(build_hasher_simple_hash_one)]
use std::hash::BuildHasher;
let b = std::collections::hash_map::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));
Run
数组的哈希值与对应的 X 像素的哈希值相同,符合实现的要求。
#![feature(build_hasher_simple_hash_one)]
use std::hash::BuildHasher;
let b = std::collections::hash_map::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));
Run