Hash
Performing a hash on a type.
Derivable: ✅
When to implement: Almost always.
#![allow(unused)] fn main() { // pub trait Hash { // // Required method // fn hash<H>(&self, state: &mut H) // where H: Hasher; // // // Provided method // fn hash_slice<H>(data: &[Self], state: &mut H) // where H: Hasher, // Self: Sized { ... } // } #[derive(Hash)] pub struct User { id: u32, name: String, friends: Vec<u32>, } }
- Allows a type to be used in hash algorithms.
- Most commonly used with data structures like
HashMap.