trait
Hash — feed a key into a hasher
Hash describes how a value contributes data to a hasher. Equal keys must produce equal hashes when hashed equivalently; unequal keys may collide. Deriving Hash, Eq and PartialEq together is a good starting point for field-based equality. Expected stdout (not executed): Some("open") Common mistake: Expecting Hash to return a unique u64 or ignoring fields used in equality when writing a custom implementation.
Formal Syntax & Structure
rust syntaxrust
pub trait Hash { fn hash<H: Hasher>(&self, state: &mut H); /* provided methods omitted */ }Working Example
Example Snippetrust
fn main() { use std::collections::HashMap; #[derive(Hash, Eq, PartialEq)] struct Ticket { zone: u8, id: u32 } let m = HashMap::from([(Ticket { zone: 2, id: 8 }, "open")]); println!("{:?}", m.get(&Ticket { zone: 2, id: 8 }));}