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