2

This article seems to imply the possibility that the use of the term "move" in the rust documentation doesn't mean copies, but transfer of ownership at compile time. See this quote specifically:

The compiler enforces that there is only a single owner. Assigning the pointer to a new location transfers ownership (known as a move for short). Consider this program:

Is this correct? are ownership transfers/moves not actually copies at runtime, but only a compile time abstraction.

4

1 回答 1

3

不,移动仍然是副本(在 的意义上memcpy),尽管不一定是整个数据结构。但是,它具有您列出的编译时语义。那是,

let a = ~[1,2,3];
let b = a; // copies one word (the pointer), "moves" the data.

let c = b.clone(); // copies the data too.

(请注意,我使用b.clone()了而不是copy b,因为Copy 正在被删除,并被替换为Clone,它更强大/更灵活。)

这种复制行为是必须发生的,因为 Rust 中的(许多)变量是确定的内存块(就像 C/C++ 中的那些),并且如果某物具有特定值,则该值必须在适当的位置内存中;这意味着移动(通常涉及将数据从一个变量传输到另一个变量)必须实际执行复制。

于 2013-07-12T11:48:42.063 回答