Wie kann ich das umsetzen Rost Kopie Eigenschaft?

Ich versuche zu initialisieren ein array von structs in Rust:

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

struct RoadPoint {
    direction: Direction,
    index: i32,
}

//Initialise the array, but failed.
let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; 

Wenn ich versuche zu kompilieren, der compiler beschwert sich, dass die Copy Merkmal nicht implementiert ist:

error[E0277]: the trait bound `main::RoadPoint: std::marker::Copy` is not satisfied
  --> src/main.rs:15:16
   |
15 |     let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; 
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `main::RoadPoint`
   |
   = note: the `Copy` trait is required because the repeated element will be copied

Wie kann die Copy trait implementiert werden?

  • #[derive(Clone, Copy)] der richtige Weg ist, aber für das Protokoll, es ist nicht magisch: Es ist einfach zu implementieren, dass die Züge manuell, insbesondere in einfachen Fällen, wie der ihrige: impl Copy for Direction {} impl Clone for Direction { fn clone(&self) -> Self { *self } }
InformationsquelleAutor tehnyit | 2016-02-17
Schreibe einen Kommentar