How to Implement Hash for Custom Structs in Rust

Implement the Hash trait for your struct to use it as a key in HashMap or HashSet.

Implement the std::hash::Hash trait for your struct to enable hashing.

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

struct Point {
    x: i32,
    y: i32,
}

impl Hash for Point {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        self.y.hash(state);
    }
}

This allows your struct to be used as a key in HashMap or stored in HashSet.