Does `&mut T` implement `Copy` or not?

1.7k views Asked by At

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.

  1. Why does calling test twice work fine?
  2. Why does not it work with test2 the same way?
  3. How can I change test2 to keep both impl-like boundary and get successful compilation?
0

There are 0 answers