How to implement the Copy trait in Rust for a custom type?
Hello everyone! I'm a beginner in Rust and trying to understand traits. I have a simple structure representing a point on a plane:
```rust
struct Point {
x: i32,
y: i32,
}
```
I want this structure to behave like a simple type that can be implicitly copied, like `i32` or `bool`. That is, I want that when assigning `let p2 = p1;`, the variable `p1` remains accessible (like with numbers), rather than being moved (like with `String` or `Vec`). I've heard that for this I need to implement the `Copy` trait. But when I try to write:
```rust
impl Copy for Point {}
```
The compiler gives an error: `the trait `Copy` cannot be implemented for this type; the type does not implement `Clone`". I don't understand why this happens. What am I doing wrong? Do I also need to implement
No answers yet.
Log in to leave an answer