根据撰写本文时的Rust reference:
对于包含实现特殊特征Drop的拥有指针或值的类型,移动变量。所有其他类型都被复制。
以下是我的代码。我希望Point
是可复制的类型。但它正在被移动,以下代码将无法以0.13.0每晚编译。
struct Point {
x: uint,
y: uint
}
fn main() {
let p: Point = Point{x: 10u, y: 10u};
let p1 = p;
let p2 = p; //Error: p has been moved p1
}
编译错误说明:
note: `p` moved here because it has type `Point`, which is moved by default
为什么Point
不被视为可复制类型?
答案 0 :(得分:10)
Copy
,指南尚未更新:
#[derive(Copy)]
struct Point {
x: uint,
y: uint
}
fn main() {
let p: Point = Point{x: 10u, y: 10u};
let p1 = p;
let p2 = p; // Now works because it is Copy.
}