How can I implement Rust's Copy trait?

后端 未结 2 1421
栀梦
栀梦 2020-12-09 07:21

I am trying to initialise an array of structs in Rust:

enum Direction {
    North,
    East,
    South,
    West,
}

struct RoadPoint {
    direction: Direct         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 08:05

    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.

提交回复
热议问题