these doesn't compile:
use std::ops::Deref;
struct M(i32);
impl Deref for M {
type Target = i32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
//fn f(_x: &i32, _y: &i32) {}
fn main() {
let a = 100;
let b = M(100);
//f(&a, &b);
let c = a.eq(&b);
println!("{}", c);
}
error[E0277]: can't compare `{integer}` with `M`
--> src/main.rs:18:15
|
18 | let c = a.eq(&b);
| ^^ no implementation for `{integer} == M`
|
= help: the trait `PartialEq<M>` is not implemented for `{integer}`
these compile:
use std::ops::Deref;
struct M(i32);
impl Deref for M {
type Target = i32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn f(_x: &i32, _y: &i32) {}
fn main() {
let a = 100;
let b = M(100);
f(&a, &b);
let c = a.eq(&b);
println!("{}", c); //true
}
How so?
I think acrossing to “ If T
implements Deref<Target = U>
, and x
is a value of type T
, then: Values of type &T
are coerced to values of type &U
” from Deref in std::ops - Rust , all should compile.