hash
import std::core::hash; · source
A Hash type feeds its bytes to a Hasher, which folds them into a running state and finally produces a u64.
import std::core::hash;
mut h: DefaultHasher = DefaultHasher::new();
h.fold(user_id);
h.fold(name);
const digest: u64 = h.finish();
Types
Hasher
type trait Hasher {
write_u8(mut &this, value: u8) -> void;
write_bytes(mut &this, bytes: Slice<u8>) -> void;
write_uint(mut &this, value: u128, byte_count: u32) -> void;
fold<T>(mut &this, value: T) -> void;
finish(&this) -> u64;
}
write_u8 is the one required method; write_bytes, write_uint, and fold have defaults built on it.
fold<T> is the composition entry point: it dispatches at compile time via static match (T) — no runtime cost and no trait bound, because the arms are the accepted types. It takes every primitive scalar, folding integers at their natural width (little-endian), floats as their IEEE bit pattern, char as its 32-bit scalar, boolean as one byte, and string as its bytes plus a separator NUL so that "ab" + "c" cannot collide with "a" + "bc". Anything non-primitive is a compile error.
Methods
write_u8(mut &this, value: u8) -> void;
write_u8(mut &this, value: u8) -> void;Feed a single byte into the hash state.
write_bytes(mut &this, bytes: Slice<u8>) -> void;
write_bytes(mut &this, bytes: Slice<u8>) -> void;Feed a byte slice. Implementations should override with a faster bulk path; the default falls back to write_u8 per byte.
write_uint(mut &this, value: u128, byte_count: u32) -> void;
write_uint(mut &this, value: u128, byte_count: u32) -> void;Feed the low byte_count bytes of value, least-significant byte first (little-endian, platform-independent). The single entry point for hashing any fixed-width integer: write_u8 is the 1-byte special case; this covers 2/4/8/16-byte values. The default loops write_u8; implementations may override with a faster path.
fold<T>(mut &this, value: T) -> void;
fold<T>(mut &this, value: T) -> void;Fold one delimited value into the running digest, dispatched at compile time via static match (T) (no runtime cost, no trait bound - the arms ARE the accepted types). Lets a digest be composed from heterogeneous typed parts (h.fold(x); h.fold(y); h.finish()) without a per-type helper name. Every primitive scalar is accepted: - integers (u8..u128, i8..i128, usize/isize) - their two's-complement bytes at NATURAL width, low byte first (LE), so a u8 folds 1 byte, a u32 4, a u64 8, a u128 16. - f32/f64 - their IEEE-754 bit pattern (via mem::transmute), so 0.0/-0.0/
finish(&this) -> u64;
finish(&this) -> u64;Finalize the hash and return the digest. After calling finish the hasher should not be written to again.
Implementors
implement trait Hasher for struct DefaultHasher
implement trait Hasher for struct Fnv128Hasher
Hash
type trait Hash {
hash<H>(&this, hasher: mut &H) -> void
where H: Hasher;
}
A type that can feed itself to any Hasher. Floats have no impl: their Eq is bitwise, and hashing the bits is a one-liner with fold when you need it.
Methods
hash<H>(&this, hasher: mut &H) -> void where H: Hasher;
Implementors
implement trait Hash for u8
implement trait Hash for u16
implement trait Hash for u32
implement trait Hash for u64
implement trait Hash for i8
implement trait Hash for i16
implement trait Hash for i32
implement trait Hash for i64
implement trait Hash for u128
implement trait Hash for i128
implement trait Hash for usize
implement trait Hash for isize
implement trait Hash for boolean
implement trait Hash for char
implement trait Hash for string
implement<T, A> trait Hash for struct Array<T, A> where T: Hash, A: Allocator
std::collections::array
implement trait Hash for struct Str
std::collections::str
implement<A> trait Hash for struct String<A> where A: Allocator
std::collections::string
DefaultHasher
type struct DefaultHasher {
state: u64;
static new() -> DefaultHasher;
static from_state(state: u64) -> DefaultHasher;
static offset_basis() -> u64;
}
64-bit FNV-1a. Fast and simple, and the right default for HashMap keys when the inputs are trusted. It is not DoS-resistant on its own; HashMap hardens it with a per-map seed. from_state lets you resume from a previously finished digest and thread a running hash through several steps.
Properties
| Property | Type |
|---|---|
state | u64 |
Constructors
static new() -> DefaultHasher;
static from_state(state: u64) -> DefaultHasher;
static from_state(state: u64) -> DefaultHasher;Resume hashing from a previously-finished u64 state. Lets an incremental fold thread a running digest through several steps (e.g. build fingerprinting) without re-deriving the FNV-1a update inline.
Static functions
static offset_basis() -> u64;
static offset_basis() -> u64;The FNV-1a offset basis: the initial state of a fresh hasher.
Fnv128Hasher
type struct Fnv128Hasher {
state: u128;
static new() -> Fnv128Hasher;
static from_state(state: u128) -> Fnv128Hasher;
finish128(&this) -> u128;
high64(&this) -> u64;
low64(&this) -> u64;
}
The same construction at twice the width, for cases where a collision is a correctness failure rather than a slow bucket. finish128() gives the full digest; high64() and low64() split it for rendering; finish() from the Hasher trait folds it to 64 bits. Still FNV, so still not hardened — this is width, not security.
Properties
| Property | Type |
|---|---|
state | u128 |
Constructors
static new() -> Fnv128Hasher;
static from_state(state: u128) -> Fnv128Hasher;
static from_state(state: u128) -> Fnv128Hasher;Resume from a previously-finish128ed state, so a digest can be threaded through several steps without re-deriving the update.
Methods
finish128(&this) -> u128;
finish128(&this) -> u128;The full 128-bit digest. finish narrows to 64 bits to satisfy Hasher; callers that want the width this type exists for use this.
high64(&this) -> u64;
high64(&this) -> u64;The digest's two halves, high first, for callers rendering it as text. Returned rather than formatted here so core::hash keeps depending on nothing but core - hex formatting lives on u64, and pulling String in for it would put the collections module underneath every HashMap that uses this file.
low64(&this) -> u64;
Free Functions
function digest<T>(value: &T) -> u64 where T: Hash;
function digest<T>(value: &T) -> u64 where T: Hash;The one-shot form: a fresh DefaultHasher, one hash, one finish.
Constants
const FNV_PRIME: u64 = 1099511628211;
const FNV_OFFSET_BASIS: u64 = 14695981039346656037;
const FNV128_PRIME: u128 = 309485009821345068724781371;
const FNV128_OFFSET_BASIS: u128 = 144066263297769815596495629667062367629;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut h: DefaultHashertype struct DefaultHasher {
state: u64;
}64-bit FNV-1a. Simple; not DoS-resistant. Fine as a HashMap default when inputs are trusted.
std::core::hashstatic DefaultHasher::new() -> DefaultHashermut h: DefaultHasherDefaultHasher::fold<T>(mut &this, value: T) -> voidvia trait Hasher
Fold one delimited value into the running digest, dispatched at compile time via static match (T) (no runtime cost, no trait bound - the arms ARE the accepted types). Lets a digest be composed from heterogeneous typed parts (h.fold(x); h.fold(y); h.finish()) without a per-type helper name.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const digest: u64type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Hasher {
write_u8(mut &this, value: u8) -> void;
write_bytes(mut &this, bytes: Slice<u8>) -> void;
write_uint(mut &this, value: u128, byte_count: u32) -> void;
fold<T>(mut &this, value: T) -> void;
finish(&this) -> u64;
}Hasher::write_u8(mut &this, value: u8) -> voidFeed a single byte into the hash state.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) value: u8Hasher::write_bytes(mut &this, bytes: Slice<u8>) -> voidFeed a byte slice. Implementations should override with a faster bulk path; the default falls back to write_u8 per byte.
(parameter) bytes: Slice<u8>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::sliceHasher::write_uint(mut &this, value: u128, byte_count: u32) -> voidFeed the low byte_count bytes of value, least-significant byte first (little-endian, platform-independent). The single entry point for hashing any fixed-width integer: write_u8 is the 1-byte special case; this covers 2/4/8/16-byte values.
(parameter) value: u128(parameter) byte_count: u32Hasher::fold<T>(mut &this, value: T) -> voidFold one delimited value into the running digest, dispatched at compile time via static match (T) (no runtime cost, no trait bound - the arms ARE the accepted types). Lets a digest be composed from heterogeneous typed parts (h.fold(x); h.fold(y); h.finish()) without a per-type helper name.
type parameter T(parameter) value: Ttype parameter THasher::finish(&this) -> u64Finalize the hash and return the digest. After calling finish the hasher should not be written to again.
type trait Hash {
hash<H>(&this, hasher: mut &H) -> void
where H: Hasher;
}Hashing.
Hash::hash<H>(&this, hasher: mut &H) -> void
where H: Hashertype parameter Hwhere H: Hasher
(parameter) hasher: mut &Hwhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Hasher {
write_u8(mut &this, value: u8) -> void;
write_bytes(mut &this, bytes: Slice<u8>) -> void;
write_uint(mut &this, value: u128, byte_count: u32) -> void;
fold<T>(mut &this, value: T) -> void;
finish(&this) -> u64;
}type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct DefaultHasher {
state: u64;
}64-bit FNV-1a. Simple; not DoS-resistant. Fine as a HashMap default when inputs are trusted.
DefaultHasher.state: u64static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static DefaultHasher::new() -> DefaultHasherstatic DefaultHasher::from_state(state: u64) -> DefaultHasherResume hashing from a previously-finished u64 state. Lets an incremental fold thread a running digest through several steps (e.g. build fingerprinting) without re-deriving the FNV-1a update inline.
(parameter) state: u64static DefaultHasher::offset_basis() -> u64The FNV-1a offset basis: the initial state of a fresh hasher.
type struct Fnv128Hasher {
state: u128;
}128-bit FNV-1a. Same construction as DefaultHasher, twice the width.
Fnv128Hasher.state: u128static Fnv128Hasher::new() -> Fnv128Hashertype struct Fnv128Hasher {
state: u128;
}128-bit FNV-1a. Same construction as DefaultHasher, twice the width.
static Fnv128Hasher::from_state(state: u128) -> Fnv128HasherResume from a previously-finish128ed state, so a digest can be threaded through several steps without re-deriving the update.
(parameter) state: u128Fnv128Hasher::finish128(&this) -> u128The full 128-bit digest. finish narrows to 64 bits to satisfy Hasher; callers that want the width this type exists for use this.
Fnv128Hasher::high64(&this) -> u64The digest's two halves, high first, for callers rendering it as text. Returned rather than formatted here so core::hash keeps depending on nothing but core - hex formatting lives on u64, and pulling String in for it would put the collections module underneath every HashMap that uses this file.
Fnv128Hasher::low64(&this) -> u64const FNV_PRIME: u64 = 1099511628211;const FNV_OFFSET_BASIS: u64 = 14695981039346656037;const FNV128_PRIME: u128 = 309485009821345068724781371;const FNV128_OFFSET_BASIS: u128 = 144066263297769815596495629667062367629;Hasher::write_u8(mut &this, value: u8) -> voidFeed a single byte into the hash state.
std::core::hashHasher::write_bytes(mut &this, bytes: Slice<u8>) -> voidFeed a byte slice. Implementations should override with a faster bulk path; the default falls back to write_u8 per byte.
Hasher::write_uint(mut &this, value: u128, byte_count: u32) -> voidFeed the low byte_count bytes of value, least-significant byte first (little-endian, platform-independent). The single entry point for hashing any fixed-width integer: write_u8 is the 1-byte special case; this covers 2/4/8/16-byte values.
Hasher::fold<T>(mut &this, value: T) -> voidFold one delimited value into the running digest, dispatched at compile time via static match (T) (no runtime cost, no trait bound - the arms ARE the accepted types). Lets a digest be composed from heterogeneous typed parts (h.fold(x); h.fold(y); h.finish()) without a per-type helper name.
static match (T) { ... }Opens a compile-time match on a type parameter: the one arm whose type matches is kept during monomorphisation and the rest are discarded.
static match (T) {
u8 | u16 => { ... }
_ => { ... }
}Selects a body by inspecting a type parameter at compile time. The subject is a type and each arm is a type (| groups several; _ takes the rest). During monomorphisation the one matching arm is kept and the others discarded, so there is no runtime branch.
primitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
primitive usizePointer-width unsigned integer; the natural type for sizes and indices.
primitive typeHasher::finish(&this) -> u64Finalize the hash and return the digest. After calling finish the hasher should not be written to again.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
Hash::hash<H>(&this, hasher: mut &H) -> void
where H: Hashertype parameter Twhere T: Hash
type parameter Awhere A: Allocator
type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatortype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmapstatic DefaultHasher::from_state(state: u64) -> DefaultHasherResume hashing from a previously-finished u64 state. Lets an incremental fold thread a running digest through several steps (e.g. build fingerprinting) without re-deriving the FNV-1a update inline.
DefaultHasher.state: u64static DefaultHasher::offset_basis() -> u64The FNV-1a offset basis: the initial state of a fresh hasher.
std::core::hashFnv128Hasher::finish128(&this) -> u128The full 128-bit digest. finish narrows to 64 bits to satisfy Hasher; callers that want the width this type exists for use this.
Fnv128Hasher::high64(&this) -> u64The digest's two halves, high first, for callers rendering it as text. Returned rather than formatted here so core::hash keeps depending on nothing but core - hex formatting lives on u64, and pulling String in for it would put the collections module underneath every HashMap that uses this file.
Fnv128Hasher::low64(&this) -> u64function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function digest<T>(value: &T) -> u64
where T: Hash(parameter) value: &T