See this code below, x == y is not allowed, but x + y is ok. What's the reason behind this design?
fn main() {
let x = 2;
let y = &x;
println!("{}", x == y);
println!("{}", x + y);
}
compile error message:
error[E0277]: can't compare `{integer}` with `&{integer}`
--> src/main.rs:4:22
|
4 | println!("{}", x == y);
| ^^ no implementation for `{integer} == &{integer}`
|
= help: the trait `PartialEq<&{integer}>` is not implemented for `{integer}`
= help: the following other types implement trait `PartialEq<Rhs>`:
This is indeed an annoying papercut. I guess at least one, maybe even the primary reason is that while PartialEq<T> can be implemented for T& and vice versa, Eq cannot (because it requires both operands have the exact same type), even though i32 == &i32 is just as total an order as i32 == i32 is. The same goes for Ord and PartialOrd.