How to implement Hash trait

Implement the Hash trait in Rust by using the #[derive(Hash)] attribute or manually defining the hash method for custom logic.

You implement the Hash trait for your custom type by using the #[derive(Hash)] attribute, which automatically generates the necessary code. If you need custom logic, you must manually implement the trait by defining the hash method and calling hasher.write on your fields.

use std::hash::{Hash, Hasher};

#[derive(Hash, PartialEq, Eq)]
struct User {
    id: u32,
    username: String,
}

// Manual implementation example (only if derive is not used)
// impl Hash for User {
//     fn hash<H: Hasher>(&self, state: &mut H) {
//         self.id.hash(state);
//         self.username.hash(state);
//     }
// }