How to implement Copy and Clone for a struct with a String field (or any non-Copy type)?
Hi everyone! I've just started learning Rust and ran into a problem that I can't solve for the second day. I'm writing a simple program for working with users, and I have this struct:
```rust
struct User {
id: u32,
name: String,
email: String,
}
```
I need this struct to be copyable and passable into functions without borrowing — that is, I want to implement the `Copy` and `Clone` traits for it. But the compiler complains when I write `#[derive(Copy, Clone)]`:
```
error[E0204]: the trait `Copy` may not be implemented for this type
--> src/main.rs:1:10
|
1 | #[derive(Copy, Clone)]
| ^^^^
2 | struct User {
3 | name: String,
| ---- this field does not implement `Copy`
```
I understand that `String` does not implement `Copy`, because
No answers yet.
Log in to leave an answer
Blogs