From How to understand the example of the Borrow trait in the Book?:
An important fact that isn’t explicitly mentioned in the book is this requirement from the
Borrow
documentation :If you are implementing
Borrow
and bothSelf
andBorrowed
implementHash
,Eq
, and/orOrd
, they must produce the same result.This isn’t enforced by the type system (so you can’t rely on it for memory safety), but it is a documented requirement for all implementers of the
Borrow
trait. This means that if you have aString
and an&str
you can hash both of them directly, without first converting them to have the same type.
Why are these invariants not enforced by the type system?