Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

rust - Copy/move semantics documentation of &T/&mut T types itself

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

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?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.
[...]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...