struct Foo {}
impl From<Foo> for u32 {
fn from(f:Foo) -> u32 {
42
}
}
fn main() {
let v = Foo{};
let a = &v;
let b = a.clone();
let c:u32 = b.into();
println!("{}",c);
}
As expected, the above code didn’t compile, but the error message
error[E0277]: the trait bound `u32: std::convert::From<&Foo>` is not satisfied
--> src/main.rs:12:19
|
12 | let c:u32 = b.into();
| ^^^^ the trait `std::convert::From<&Foo>` is not implemented for `u32`
|
= help: the following implementations were found:
<u32 as std::convert::From<Foo>>
<u32 as std::convert::From<std::net::Ipv4Addr>>
<u32 as std::convert::From<char>>
<u32 as std::convert::From<u16>>
<u32 as std::convert::From<u8>>
= note: required because of the requirements on the impl of `std::convert::Into<u32>` for `&Foo`
didn’t show the root cause: the type Foo
didn’t implement Clone
.
As Clone
is really very commonly used, I think it would be a good idea to produce a lint against the line
let b = a.clone();
with a message like
Attampt to clone on a reference that the referenced type didn't implement Clone.
References can be copied so the use of clone is redundent.