January 2019
Intermediate to advanced
316 pages
8h 8m
English
Both HashMap and HashSet use a hashing algorithm to produce the unique key required for storing and retrieving values. Hashes are created with an instance of the Hasher trait (DefaultHasher if nothing is specified) for each key that implements the Hash and Eq traits. They allow a Hasher instance to be passed into the Hash implementor to generate the required output and the data structure to compare keys for equality.
If a custom structure is to be used as a hashed key (for the map, or simply to store in the set), this implementation can be derived as well, which adds every field of the structure to the Hasher's state. In case the trait is implemented by hand, it has to create equal hashes whenever two keys are equal.