Module core::clone 1.0.0[−][src]
The Clone
trait for types that cannot be 'implicitly copied'.
In Rust, some simple types are "implicitly copyable" and when you
assign them or pass them as arguments, the receiver will get a copy,
leaving the original value in place. These types do not require
allocation to copy and do not have finalizers (i.e. they do not
contain owned boxes or implement Drop
), so the compiler considers
them cheap and safe to copy. For other types copies must be made
explicitly, by convention implementing the Clone
trait and calling
the clone
method.
Basic usage example:
let s = String::new(); // String type implements Clone let copy = s.clone(); // so we can clone itRun
To easily implement the Clone trait, you can also use
#[derive(Clone)]
. Example:
#[derive(Clone)] // we add the Clone trait to Morpheus struct struct Morpheus { blue_pill: f32, red_pill: i64, } fn main() { let f = Morpheus { blue_pill: 0.0, red_pill: 0 }; let copy = f.clone(); // and now we can clone it! }Run
Traits
Clone |
A common trait for the ability to explicitly duplicate an object. |