struct Rec { width : u32, length : u32 } fn process(rec1: Rec) -> Rec { let mut rec2 = rec1; rec2.width = 10; rec2.length = 11; rec2 } fn main() { let rec = Rec{width : 4, length : 16}; // rec.width = 100; // rec.length = 10; println!("{},{}", rec.width, rec.length); let mut rec2 =rec.clone(); rec2.width = 100; rec2.length = 10; println!("{},{}", rec2.width, rec2.length); let rec3 = process(rec); println!("{},{}", rec3.width, rec3.length); }
[root@bogon clone]# cargo build Compiling own v0.1.0 (/data2/rust/clone) error[E0599]: no method named `clone` found for struct `Rec` in the current scope --> src/main.rs:16:22 | 1 | struct Rec { | ---------- method `clone` not found for this ... 16 | let mut rec2 =rec.clone(); | ^^^^^ method not found in `Rec` | = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `std::clone::Clone` error: aborting due to previous error For more information about this error, try `rustc --explain E0599`. error: could not compile `own`. To learn more, run the command again with --verbose