When I try to compile this code:
mod module {
impl<T: Trait> Trait for &mut T {
fn method(self) {}
}
pub trait Trait {
fn method(self);
}
}
fn test<T: module::Trait>(object: &mut T) {
// use module::Trait; // uncomment to get rid of the error
object.method();
// or do this instead
// module::Trait::method(object);
}
I get the following error: error[E0507]: cannot move out of `*object` which is behind a mutable reference
. Basically, rust tries to call method
from Trait
, instead of method
from the implementation block for &mut Trait
. The most confusing thing for me here is that adding use module::Trait
gets rid of the error.
So, why is there an error in the first place, and how does adding a use declaration help?