January 2019
Beginner to intermediate
554 pages
13h 31m
English
The Clone trait is for explicit duplication and comes with a clone method that a type can implement to obtain a copy of itself. The Clone trait is defined like so:
pub trait Clone { fn clone(&self) -> Self;}
It has a method called clone that takes an immutable reference to the receiver, that is, &self, and returns a new value of the same type. User defined types or any wrapper types that need to provide the ability to duplicate themselves should implement the Clone trait by implementing the clone method.
But unlike Copy types where assignment implicitly copies the value, to duplicate a Clone value, we have to explicitly call the clone method. The clone method is a more general duplication mechanism and Copy is a special case of it, ...
Read now
Unlock full access