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 Ipv6Addrimpl Hash for Localityimpl<T: PointeeSized> Hash for PhantomData<T>impl<'a> Hash for PhantomInvariantLifetime<'a>impl<T: Hash, N: usize> Hash for [T; N]impl Hash for u16impl Hash for u64impl Hash for i8impl<Idx: $crate::hash::Hash> Hash for Range<Idx>impl Hash for i32impl Hash for SocketAddrV6impl Hash for isizeimpl Hash for Orderingimpl<T: ?Sized + Hash> Hash for ManuallyDrop<T>impl Hash for i128impl Hash for strimpl<'a> Hash for PhantomContravariantLifetime<'a>impl<T: Hash> Hash for (T)impl<T: ?Sized + marker::PointeeSized + Hash> Hash for &mut Timpl<T: $crate::hash::Hash> Hash for Bound<T>impl<T: $crate::hash::Hash> Hash for Reverse<T>impl Hash for Durationimpl<T, N: usize> Hash for Simd<T, N>impl<Idx: $crate::hash::Hash> Hash for RangeTo<Idx>impl Hash for ByteStrimpl<F> Hash for fn(_: T) -> Retimpl Hash for AsciiCharimpl Hash for Errorimpl<B: $crate::hash::Hash, C: $crate::hash::Hash> Hash for ControlFlow<B, C>impl<T> Hash for Exclusive<T>impl<Y: $crate::hash::Hash, R: $crate::hash::Hash> Hash for CoroutineState<Y, R>impl<Idx: $crate::hash::Hash> Hash for RangeInclusive<Idx>impl<F: FnPtr> Hash for Fimpl<Ptr: Deref<Target: Hash>> Hash for Pin<Ptr>impl<T: PointeeSized> Hash for NonNull<T>impl Hash for Location<'_>impl<'a> Hash for PhantomCovariantLifetime<'a>impl Hash for PhantomPinnedimpl Hash for CharCaseimpl Hash for Infallibleimpl Hash for charimpl<Dyn: PointeeSized> Hash for DynMetadata<Dyn>impl Hash for ()impl<T> Hash for NonZero<T>impl Hash for CStrimpl<T: ?Sized + marker::PointeeSized + Hash> Hash for &Timpl<T: $crate::hash::Hash, E: $crate::hash::Hash> Hash for Result<T, E>impl<T: ?Sized + marker::PointeeSized> Hash for *mut Timpl Hash for Layoutimpl<Idx: $crate::hash::Hash> Hash for RangeToInclusive<Idx>impl<T: $crate::hash::Hash> Hash for Saturating<T>impl Hash for u8impl Hash for u32impl Hash for usizeimpl Hash for Orderingimpl Hash for i16impl<T> Hash for PhantomCovariant<T>impl Hash for i64impl Hash for u128impl Hash for SocketAddrimpl<T> Hash for PhantomContravariant<T>impl Hash for Alignmentimpl<T: $crate::hash::Hash> Hash for Option<T>impl Hash for Ipv4Addrimpl<T: $crate::hash::Hash> Hash for Wrapping<T>impl<T> Hash for PhantomInvariant<T>impl Hash for RangeFullimpl<Idx: $crate::hash::Hash> Hash for Range<Idx>impl Hash for boolimpl Hash for neverimpl<T: Hash> Hash for [T]impl<Idx: $crate::hash::Hash> Hash for RangeToInclusive<Idx>impl<T: ?Sized + marker::PointeeSized> Hash for *const Timpl<Idx: $crate::hash::Hash> Hash for RangeInclusive<Idx>impl<T: $crate::hash::Hash> Hash for Poll<T>impl<T> Hash for Discriminant<T>impl Hash for Ipv6MulticastScopeimpl Hash for SocketAddrV4impl Hash for TypeIdimpl Hash for IntErrorKindimpl<Idx: $crate::hash::Hash> Hash for RangeFrom<Idx>impl Hash for IpAddrimpl<Idx: $crate::hash::Hash> Hash for RangeFrom<Idx>