The main difference is that cloning is explicit. Implicit notation means moving for a non- Copy type.
// u8 implements Copy let x: u8 = 123; let y = x; // x can still be used println!("x={}, y={}", x, y); // Vec<u8> implements Clone, but not Copy let v: Vec<u8> = vec![1, 2, 3]; let w = v.clone(); //let w = v // This would *move* the value, rendering v unusable.
By the way, each type of Copy should also be Clone . However, they are not required to do the same! For your own types, .clone() can be an arbitrary method of your choice, while implicit copying always starts the implementation of memcpy , not clone(&self) .
mdup Jun 23 '15 at 20:38 2015-06-23 20:38
source share