What function does #[derive(Hash)] use?

What hash function / algorithm does #[derive(Hash)] use by default?

None. The Hash trait is generic for any Hasher.

2 Likes

This definitely correctly answers the question I asked. I intended to ask a different question (but still not sure how to formulate it). The write_u8, write_u16, ... functions calls the write function of DefaultHasher in std::collections::hash_map - Rust , which appears to be using SipHasher in std::hash - Rust ?

I completely agree with @cuviper 's point that the hash function depends on the impl of the trait. I think what I should have asked is what is used to hash &[u8] .

Is the answer for &[u8] siphash-2-4 ( https://crates.io/crates/siphasher ) ? Or is it now using something else ?

There are multiple generic levels involved here.

  • Hash for &T (like &[u8]) simply forwards to the inner type.
  • Hash for [T] (like [u8]) calls <Hash for T>::hash_slice.
  • <Hash for u8>::hash_slice calls Hasher::write for the entire slice.

For the actual Hasher, yes the default for HashMap is currently a sip-hash, although you can choose something else with the S type parameter. The implementation of that default hasher is internal to the standard library, not using the crate you mentioned.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.