Search code examples
rusttraits

Where do the trait bounds of `HashMap` actually come from?


As mentioned in the documentation of HashMap the key type K needs to implement Hash, PartialEq, and Eq which makes sense.

However, I would have expected that these trait bounds actually also show up somewhere in the documentation next to either the struct definition itself, or as trait bounds to the impl or so. But skimming over the source the only occurrence of e.q. PartialEq I can see is this one, which isn't a trait bound, and I could spot these trait bounds in the whole source.

Out of sheer curiosity: Where do these trait bounds actually come from?


Solution

  • Eq and Hash are on almost all methods, and PartialEq is a supertrait of Eq, so it is not needed to be specified explicitly.