# Module std::hash [−] [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 `#[derive(Hash)]`:

# Examples

fn main() { use std::hash::{Hash, SipHasher, Hasher}; #[derive(Hash)] struct Person { id: u32, 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(&person1) != hash(&person2)); fn hash<T: Hash>(t: &T) -> u64 { let mut s = SipHasher::new(); t.hash(&mut s); s.finish() } }

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

    #[derive(Hash)]
    struct Person {
        id: u32,
        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(&person1) != hash(&person2));

    fn hash<T: Hash>(t: &T) -> u64 {
        let mut s = SipHasher::new();
        t.hash(&mut s);
        s.finish()
    }

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

fn main() { use std::hash::{Hash, Hasher, SipHasher}; struct Person { id: u32, #[allow(dead_code)] name: String, phone: u64, } impl Hash for Person { fn hash<H: Hasher>(&self, state: &mut H) { 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_eq!(hash(&person1), hash(&person2)); fn hash<T: Hash>(t: &T) -> u64 { let mut s = SipHasher::new(); t.hash(&mut s); s.finish() } }

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

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

    impl Hash for Person {
        fn hash<H: Hasher>(&self, state: &mut H) {
            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_eq!(hash(&person1), hash(&person2));

    fn hash<T: Hash>(t: &T) -> u64 {
        let mut s = SipHasher::new();
        t.hash(&mut s);
        s.finish()
    }

## Structs

BuildHasherDefault | A structure which implements `BuildHasher` for all `Hasher` types which also implement `Default`.
SipHasher | An implementation of SipHash 2-4.

## Traits

BuildHasher | A `BuildHasher` is typically used as a factory for instances of `Hasher` which a `HashMap` can then use to hash keys independently.
Hash | A hashable type.
Hasher | A trait which represents the ability to hash an arbitrary stream of bytes.
HashState | [Deprecated] Deprecated, renamed to `BuildHasher`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize`)