`&mut T` 是否实现了 `Copy`?

问题描述

考虑这个代码

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
}

根据 Rust 的 docs,可变引用不实现 copy

  1. 为什么调用 test 两次可以正常工作?
  2. 为什么它与 test2 的工作方式不同?
  3. 如何更改 test2 以保持类似 impl 的边界并获得成功编译?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)