Skip to content

std::hashable

Import with import std::hashable.

Interfaces

interface Hashable
    fn hash(value: self) -> Int
end

Protocol for producing an Int hash that respects structural equality.

Hashable.hash(value) returns an Int such that Equatable.equal(a, b) implies Hashable.hash(a) == Hashable.hash(b). Map and Set find a key by it. The hash is not cryptographic and may change between runs of a program, so use it only inside one run.

The primitive types Int, Float, Bool and String implement Hashable, and so does Unit, whose one value always hashes the same. To implement it for a custom type:

pub struct Point
    x: Int
    y: Int
end

impl Hashable for Point
    fn hash(p: Point) -> Int
        Hashable.hash(p.x) + Hashable.hash(p.y) * 31
    end
end

derive Hashable synthesizes a structural hash for structs (mixing all field hashes), enums (mixing the variant tag and payload), and distinct types (delegating to the inner value).

fn hash(value: self) -> Int

interface Hashable