Movatterモバイル変換


[0]ホーム

URL:


Module hash

std

Modulehash 

1.0.0 ·Source
Expand description

Generic hashing support.

This module provides a generic way to compute thehash of a value.Hashes are most commonly used withHashMap andHashSet.

The simplest way to make a type hashable is to use#[derive(Hash)]:

§Examples

usestd::hash::{DefaultHasher, Hash, Hasher};#[derive(Hash)]structPerson {    id: u32,    name: String,    phone: u64,}letperson1 = Person {    id:5,    name:"Janet".to_string(),    phone:555_666_7777,};letperson2 = Person {    id:5,    name:"Bob".to_string(),    phone:555_666_7777,};assert!(calculate_hash(&person1) != calculate_hash(&person2));fncalculate_hash<T: Hash>(t:&T) -> u64 {letmuts = DefaultHasher::new();    t.hash(&muts);    s.finish()}

If you need more control over how a value is hashed, you need to implementtheHash trait:

usestd::hash::{DefaultHasher, Hash, Hasher};structPerson {    id: u32,    name: String,    phone: u64,}implHashforPerson {fnhash<H: Hasher>(&self, state:&mutH) {self.id.hash(state);self.phone.hash(state);    }}letperson1 = Person {    id:5,    name:"Janet".to_string(),    phone:555_666_7777,};letperson2 = Person {    id:5,    name:"Bob".to_string(),    phone:555_666_7777,};assert_eq!(calculate_hash(&person1), calculate_hash(&person2));fncalculate_hash<T: Hash>(t:&T) -> u64 {letmuts = DefaultHasher::new();    t.hash(&muts);    s.finish()}

Structs§

BuildHasherDefault
Used to create a defaultBuildHasher instance for types that implementHasher andDefault.
DefaultHasher
The defaultHasher used byRandomState.
RandomState
RandomState is the default state forHashMap types.
SipHasherDeprecated
An implementation of SipHash 2-4.

Traits§

BuildHasher
A trait for creating instances ofHasher.
Hash
A hashable type.
Hasher
A trait for hashing an arbitrary stream of bytes.

Derive Macros§

Hash
Derive macro generating an impl of the traitHash.

[8]ページ先頭

©2009-2026 Movatter.jp