&'a str implements PartialEq for String[1], so you can compare a string slice with a string directly. HashMap<&'a str, V> does not implement PartialEq for HashMap<String, V>, which makes them not comparable directly.
And vice versa, but the implementation used in your example is the one from &'a str. ↩︎
Hi! Thank you!
As I see it, in Rust it will not be correct to compare different types unless they belong to the same group. I hope I understood correctly.
assert_eq!(1u8, 1i8 as u8);
// wrong! assert_eq!(1u8, 1i8);
Most operators in Rust like == use traits under the hood. You can find these traits in std::ops or for comparison operators in std::cmp. The == operator uses the PartialEq trait. An expression lhs == rhs is basically syntactic sugar for calling <lhs as PartialEq<Rhs>>::eq(&rhs), where Rhs is the type of rhs. As long as you implement PartialEq<Rhs> for Lhs—the type of the lhs variable—, you can compare values of Lhs to values of Rhs, even when Lhs is not the same type as Rhs. Integer types in Rust only implement PartialEq<Self>, which means u8 instances are only comparable to other u8 instances, u16 instances to other u16 instances and so forth.
Thanks for explaining how it works under the hood.
So, in principle, it can be implemented for tuples and hash tables and other data? It's just that these implementations are not yet in the standard library?
Strictly speaking, it's not that they are not yet in the standard library - I guess in a lot of cases they are impossible, or at least undesirable, to add to a standard library, due to ambiguities being created.