YjyJeff
YjyJeff

Reputation: 913

Why does hashing the same vector twice get different hash codes?

I want to get the hash code of this vector:

let vec = vec![1,2,3];
let tmp = vec![1,2,3];
let mut hash = DefaultHasher::new();
vec.hash(&mut hash);
println!("{}", hash.finish());
tmp.hash(&mut hash);
println!("{}", hash.finish());

However the output is:

13585085576907263210
8618793686565720431

What's going on? I have tried multiple times and always get the same result. I want the vector with the same elements to hash to the same hash code.

Upvotes: 6

Views: 3605

Answers (1)

bluss
bluss

Reputation: 13782

The hasher is not reset after finish. Create a new hasher state with DefaultHasher::new() for each hash value you want to compute.

Upvotes: 15

Related Questions