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:
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>
}