&T/&mut T 类型本身的复制/移动语义文档 [英] Copy/move semantics documentation of &T/&mut T types itself

查看:47
本文介绍了&T/&mut T 类型本身的复制/移动语义文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找关于引用和可变引用类型的复制/移动语义的文档.

I'm looking for the document about copy/move semantics of reference and mutable reference types.

以下代码片段显示不可变引用 (& T) 实现了 Copy trait 和可变引用 (&mut T)不是.

The following code snippet shows immutable references (& T) implement the Copy trait and mutable references (&mut T) do not.

struct T;
fn copyable<U>(_: U) where U: Copy {}

fn main() {
    let a = &T;
    copyable(a);  // OK

    let b = &mut T;
    copyable(b);
    // error: the trait `core::marker::Copy` is not implemented for the type `&mut T`
}

但我找不到这种行为的描述.有人知道一些(非)官方文件吗?(还是我错了?)

But I can't find the description of this behavior. Someone know some (un)official documents? (or am I wrong?)

推荐答案

Rust 的 std::marker::Copy trait 参考说(感谢 @Chris Emerson):

Rust's std::marker::Copy trait reference says (thanks to @Chris Emerson):

我的类型什么时候不能被复制?
某些类型无法安全复制.例如,复制 &mut T 会创建一个别名可变引用,而复制 String 会导致两次尝试释放同一个缓冲区.
[...]

When can my type not be Copy?
Some types can't be copied safely. For example, copying &mut T would create an aliased mutable reference, and copying String would result in two attempts to free the same buffer.
[...]

这篇关于&amp;T/&amp;mut T 类型本身的复制/移动语义文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆