Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

the trait `Copy` may not be implemented for this type when implement in rust

When I added copy in the rust struct like this:

fn main() {

}

#[derive(Debug, Default, Copy)]
pub struct BillRecord {
    pub id: Option<i64>,
    pub remark: Option<String>
}

shows error info when compile:

âžś  rust-learn git:(group-by) âś— cargo build
   Compiling rust-learn v0.1.0 (/Users/xiaoqiangjiang/source/reddwarf/backend/rust-learn)
error[E0204]: the trait `Copy` may not be implemented for this type
  --> src/main.rs:7:26
   |
7  | #[derive(Debug, Default, Copy)]
   |                          ^^^^
...
10 |     pub remark: Option<String>
   |     -------------------------- this field does not implement `Copy`
   |
note: the `Copy` impl for `std::option::Option<std::string::String>` requires that `std::string::String: Copy`
  --> src/main.rs:10:5
   |
10 |     pub remark: Option<String>
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0204`.
error: could not compile `rust-learn` due to previous error

what should I do to implement the Copy? I just want to implement the Copy trait, did not want to implement the Clone trait. this code block need to implement the Copy trait:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fn main() {
    let source = &BillRecord{ id: None, remark: None };
    let a = BillRecordResponse{ id: source.id, remark: source.remark };
}

#[derive(Debug, Serialize, Default, Clone)]
pub struct BillRecord {
    pub id: Option<i64>,
    pub remark: Option<String>
}

use serde::Serialize;

#[derive(Debug, Serialize, Default)]
pub struct BillRecordResponse {
    pub id: Option<i64>,
    pub remark: Option<String>
}

when I compile this code block, shows error:

error[E0507]: cannot move out of `source.remark` which is behind a shared reference
 --> src/main.rs:5:56
  |
5 |     let a = BillRecordResponse{ id: source.id, remark: source.remark };
  |                                                        ^^^^^^^^^^^^^ move occurs because `source.remark` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
5 |     let a = BillRecordResponse{ id: source.id, remark: source.remark.as_ref() };
  |                                                                     +++++++++

For more information about this error, try `rustc --explain E0507`.
warning: `rust-learn` (bin "rust-learn") generated 1 warning
error: could not compile `rust-learn` due to previous error; 1 warning emitted

>Solution :

The error message is saying that String does not implement Copy (so Option<String> does not either), so therefore Copy cannot be derived for your struct (which contains an Option<String>).

As Copy signifies cheap copyability, and a String may contain a lot of characters (making it not cheaply copyable), String should not implement Copy. Therefore you should not try to implement Copy, but instead implement Clone and use clone() where necessary:

fn main() {
    let source = &BillRecord{ id: None, remark: None };
    let a = BillRecordResponse{ id: source.id, remark: source.remark.clone() };
}

#[derive(Debug, Serialize, Default, Clone)]
pub struct BillRecord {
    pub id: Option<i64>,
    pub remark: Option<String>
}

use serde::Serialize;

#[derive(Debug, Serialize, Default, Clone)]
pub struct BillRecordResponse {
    pub id: Option<i64>,
    pub remark: Option<String>
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading