The std documentation is clear that the default hashing algorithm is not specified, so I cannot rely on it being the same across releases. But can I rely on it being the same across targets for the same release? The case I have in mind is when generating a static hash table during the build, where the hash function must match across the two platforms in case of cross-compilation.
I think the answer is no. The default hasher is randomized. So even with the same binary on the same platform you will get different hashes in different runs.
Oh, I didn't realize that. Thanks!
Itβs possible to use a different, non-randomized hash function (Hasher implementation). However, this does not give you portability across platforms, because the behavior of Hash::hash_slice() and of the Hash implementations of built-in types is explicitly not guaranteed.
Portability
Due to differences in endianness and type sizes, data fed by
Hashto aHashershould not be considered portable across platforms. Additionally the data passed by most standard library types should not be considered stable between compiler versions.
So, you need to:
- Convert your key type to bytes explicitly, not using
Hash. - Feed those bytes to a deterministic hash function.
Rust doesn't have any standard randomization function in my understanding. So, I wrote my own, but now I can use just the standard hashing algorithm for the purpose. Thank you for the tip.