Consider this code:
struct A;
trait Test {
fn test(self);
}
impl Test for &mut A {
fn test(self) {
println!("&mut Test called");
}
}
fn test(a: &mut A) {
a.test();
}
fn test2(a: impl Test) {
a.test();
}
fn main() {
let mut a = A;
let a_mut_ref = &mut a;
test(a_mut_ref); // works fine
test(a_mut_ref); // works fine
test2(a_mut_ref); // `a_mut_ref` moved here? Why?
test2(a_mut_ref); // compile error `use of moved value` here
}
According to Rust's docs, mutable references do not implement Copy
.
- Why does calling
test
twice work fine? - Why does not it work with
test2
the same way? - How can I change
test2
to keep both impl-like boundary and get successful compilation?