How to use std :: hash :: hash?

I am trying to play with the Rust function std::hash:

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

#[test]
fn hash_test() {
    println!("{}", hash::<_, SipHasher>(&"hello"));
}

I get this error:

error: use of unstable library feature 'hash': module was recently redesigned

My version of Rust:

rustc 1.0.0-beta (9854143cb 2015-04-02) (built 2015-04-02)

Is this syntax no longer valid?

+4
source share
1 answer

The initial question was to use an unstable function, which means that it cannot be used in a stable release, such as 1.0-beta or 1.0. With their help, this function has been removed from the language.

- , -. , SipHasher . , crates.io. :

use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;

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

fn my_hash<T>(obj: T) -> u64
where
    T: Hash,
{
    let mut hasher = DefaultHasher::new();
    obj.hash(&mut hasher);
    hasher.finish()
}

fn main() {
    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,
    };

    println!("{}", my_hash(person1));
    println!("{}", my_hash(person2));
}
+3

All Articles