I am trying to play with Rust's std::hash
function:
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 Rust version is:
rustc 1.0.0-beta (9854143cb 2015-04-02) (built 2015-04-02)
Is this syntax no longer valid?
The original question was trying to use a feature that was unstable - which means it isn't allowed to be used in a stable release, like 1.0-beta or 1.0. Since them, the function has been removed from the language.
The replacement is to write your own version, which allows you to specify the hash function. Additionally, using SipHasher
directly is deprecated. If you need a specific hashing algorithm, you should pull it from crates.io. Otherwise, you can use an unspecified hashing algorithm from the standard library:
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With