Trait Hash
trait Hash: marker::PointeeSized
A hashable type.
Types implementing Hash are able to be hashed with an instance of
Hasher.
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 calling
hash on each field.
If you need more control over how a value is hashed, you can of course
implement the Hash trait yourself:
use ;
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 extra
0xFF 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 Hasher
should 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.
Required Methods
fn hash<H: Hasher>(self: &Self, state: &mut H)Feeds this value into the given
Hasher.Examples
use ; let mut hasher = new; 7920.hash; println!;
Provided Methods
fn hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: SizedFeeds 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
hashand implementations ofHashshould keep that in mind and callhashthemselves if the slice isn't treated as a whole unit in thePartialEqimplementation.For example, a
VecDequeimplementation might naïvely callas_slicesand thenhash_sliceon each slice, but this is wrong since the two slices can change with a call tomake_contiguouswithout affecting thePartialEqresult. Since these slices aren't treated as singular units, and instead part of a larger deque, this method cannot be used.Examples
use ; let mut hasher = new; let numbers = ; hash_slice; println!;
Implementors
impl Hash for AsciiCharimpl<'a> Hash for PhantomInvariantLifetime<'a>impl<Ptr: Deref<Target: Hash>> Hash for Pin<Ptr>impl<Idx: $crate::hash::Hash> Hash for RangeToInclusive<Idx>impl<T> Hash for PhantomCovariant<T>impl Hash for neverimpl Hash for i8impl<T: $crate::hash::Hash> Hash for Saturating<T>impl Hash for Alignmentimpl<T: ?Sized + marker::PointeeSized> Hash for *mut Timpl<Idx: $crate::hash::Hash> Hash for Range<Idx>impl<'a> Hash for PhantomContravariantLifetime<'a>impl Hash for Location<'_>impl Hash for RangeFullimpl Hash for Localityimpl<'a> Hash for PhantomCovariantLifetime<'a>impl Hash for u16impl Hash for ()impl Hash for CStrimpl Hash for u128impl<T> Hash for NonZero<T>impl Hash for Durationimpl Hash for Ipv4Addrimpl<F> Hash for fn(_: T) -> Retimpl Hash for ByteStrimpl Hash for i16impl<B: $crate::hash::Hash, C: $crate::hash::Hash> Hash for ControlFlow<B, C>impl Hash for Ipv6MulticastScopeimpl<Idx: $crate::hash::Hash> Hash for RangeFrom<Idx>impl<T: Hash> Hash for (T)impl Hash for Ipv6Addrimpl<Idx: $crate::hash::Hash> Hash for RangeInclusive<Idx>impl<Idx: $crate::hash::Hash> Hash for RangeFrom<Idx>impl<T> Hash for Exclusive<T>impl Hash for IpAddrimpl Hash for u32impl<T: $crate::hash::Hash> Hash for Poll<T>impl<T: $crate::hash::Hash> Hash for Wrapping<T>impl Hash for i128impl<T: PointeeSized> Hash for NonNull<T>impl<T: Hash> Hash for [T]impl<T: PointeeSized> Hash for PhantomData<T>impl Hash for i32impl Hash for Layoutimpl<F: FnPtr> Hash for Fimpl<Y: $crate::hash::Hash, R: $crate::hash::Hash> Hash for CoroutineState<Y, R>impl<Dyn: PointeeSized> Hash for DynMetadata<Dyn>impl Hash for boolimpl<Idx: $crate::hash::Hash> Hash for RangeInclusive<Idx>impl Hash for IntErrorKindimpl Hash for u64impl Hash for SocketAddrV6impl<T: ?Sized + marker::PointeeSized + Hash> Hash for &Timpl Hash for Errorimpl<Idx: $crate::hash::Hash> Hash for RangeToInclusive<Idx>impl<T> Hash for PhantomInvariant<T>impl<T, N: usize> Hash for Simd<T, N>impl<T: Hash, N: usize> Hash for [T; N]impl<T: $crate::hash::Hash, E: $crate::hash::Hash> Hash for Result<T, E>impl<Idx: $crate::hash::Hash> Hash for Range<Idx>impl<T: $crate::hash::Hash> Hash for Option<T>impl Hash for charimpl<T: ?Sized + Hash> Hash for ManuallyDrop<T>impl Hash for Orderingimpl Hash for i64impl<T: $crate::hash::Hash> Hash for Bound<T>impl Hash for TypeIdimpl Hash for SocketAddrimpl<T: ?Sized + marker::PointeeSized + Hash> Hash for &mut Timpl<T: $crate::hash::Hash> Hash for Reverse<T>impl<Idx: $crate::hash::Hash> Hash for RangeTo<Idx>impl Hash for usizeimpl<T> Hash for PhantomContravariant<T>impl<T> Hash for Discriminant<T>impl Hash for Infallibleimpl Hash for strimpl Hash for PhantomPinnedimpl Hash for u8impl<T: ?Sized + marker::PointeeSized> Hash for *const Timpl Hash for isizeimpl Hash for Orderingimpl Hash for SocketAddrV4