Hi there!
I just ran into a compiler error that really confused me. After some time I found out where my mistake was, but I think this could confuse many others. So here is some sample code: Rust Playground
struct Foo;
impl Into<bool> for Foo {
fn into(self) -> bool {
true
}
}
impl Foo {
pub fn do_something(&self) {
let b : bool = self.into();
}
}
The compiler says:
<anon>:11:29: 11:35 error: the trait `core::convert::From<&Foo>` is not implemented for the type `bool` [E0277]
<anon>:11 let b : bool = self.into();
^~~~~~
So first of all the compiler automagically mentions From
, which is kinda confusing, if one doesn't know that Into
is implemented for all From
s. Even if you know that, you still think that the compiler says "Into is not implemented for Foo".
The real problem with the code is, that self
in do_something
is a reference, but into
takes self
by value.
I think its highly confusing. One is used to be able to call methods on references and values directly, thanks to auto-deref. Why isn't this auto-dereffed here? Then there would be an error message like "cannot move out of borrowed value" which immediately points in the right direction.
So: Is this the way it should be? If yes: Why? If no: Are the language devs aware of that?