How can I implement Rust’s Copy trait?

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.

Leave a Comment