为什么我的隐式Copyable变量被移动了?

时间:2014-12-23 21:49:48

标签: rust

根据撰写本文时的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不被视为可复制类型?

1 个答案:

答案 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.
}