Module std::hashExperimental [-]  [+] [src]

Generic hashing support.

This module provides a generic way to compute the hash of a value. The simplest way to make a type hashable is to use #[deriving(Hash)]:

Example

fn main() { use std::hash; use std::hash::Hash; #[deriving(Hash)] struct Person { id: uint, name: String, phone: u64, } let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 }; let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 }; assert!(hash::hash(&person1) != hash::hash(&person2)); }
use std::hash;
use std::hash::Hash;

#[deriving(Hash)]
struct Person {
    id: uint,
    name: String,
    phone: u64,
}

let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };

assert!(hash::hash(&person1) != hash::hash(&person2));

If you need more control over how a value is hashed, you need to implement the trait Hash:

fn main() { use std::hash; use std::hash::Hash; use std::hash::sip::SipState; struct Person { id: uint, name: String, phone: u64, } impl Hash for Person { fn hash(&self, state: &mut SipState) { self.id.hash(state); self.phone.hash(state); } } let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 }; let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 }; assert!(hash::hash(&person1) == hash::hash(&person2)); }
use std::hash;
use std::hash::Hash;
use std::hash::sip::SipState;

struct Person {
    id: uint,
    name: String,
    phone: u64,
}

impl Hash for Person {
    fn hash(&self, state: &mut SipState) {
        self.id.hash(state);
        self.phone.hash(state);
    }
}

let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };

assert!(hash::hash(&person1) == hash::hash(&person2));

Modules

sip

An implementation of SipHash 2-4.

Structs

RandomSipHasher

RandomSipHasher computes the SipHash algorithm from a stream of bytes initialized with random keys.

Traits

Hash

A hashable type. The S type parameter is an abstract hash state that is used by the Hash to compute the hash. It defaults to std::hash::sip::SipState.

Hasher

A trait that computes a hash for a value. The main users of this trait are containers like HashMap, which need a generic way hash multiple types.

Writer

Functions

hash

Hashes a value using the SipHash algorithm.