How can I implement Rust's Copy trait?

爷,独闯天下 提交于 2019-11-29 09:12:37

You don't have to implement Copy yourself; the compiler can derive it for you:

#[derive(Copy, Clone)]
enum Direction {
    North,
    East,
    South,
    West,
}

#[derive(Copy, Clone)]
struct RoadPoint {
    direction: Direction,
    index: i32,
}

Note that every type that implements Copy must also implement Clone. Clone can also be derived.

Just prepend #[derive(Copy, Clone)] before your enum.

If you really want, you can also

impl Copy for MyEnum {}

The derive-attribute does the same thing under the hood.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!