Trait std::clone::Clone1.0.0 [−] [src]

#[lang = "clone"]
pub trait Clone { fn clone(&self) -> Self; fn clone_from(&mut self, source: &Self) { ... } }

A common trait for the ability to explicitly duplicate an object.

Differs from Copy in that Copy is implicit and extremely inexpensive, while Clone is always explicit and may or may not be expensive. In order to enforce these characteristics, Rust does not allow you to reimplement Copy, but you may reimplement Clone and run arbitrary code.

Since Clone is more general than Copy, you can automatically make anything Copy be Clone as well.

Derivable

This trait can be used with #[derive] if all fields are Clone. The derived implementation of clone calls clone on each field.

How can I implement Clone?

Types that are Copy should have a trivial implementation of Clone. More formally: if T: Copy, x: T, and y: &T, then let x = y.clone(); is equivalent to let x = *y;. Manual implementations should be careful to uphold this invariant; however, unsafe code must not rely on it to ensure memory safety.

An example is an array holding more than 32 elements of a type that is Clone; the standard library only implements Clone up until arrays of size 32. In this case, the implementation of Clone cannot be derived, but can be implemented as:

#[derive(Copy)]
struct Stats {
   frequencies: [i32; 100],
}

impl Clone for Stats {
    fn clone(&self) -> Stats { *self }
}Run

Required Methods

Returns a copy of the value.

Examples

let hello = "Hello"; // &str implements Clone

assert_eq!("Hello", hello.clone());Run

Provided Methods

Performs copy-assignment from source.

a.clone_from(&b) is equivalent to a = b.clone() in functionality, but can be overridden to reuse the resources of a to avoid unnecessary allocations.

Implementations on Foreign Types

impl Clone for Isaac64Rng
[src]

[src]

1.0.0
[src]

impl Clone for ReseedWithDefault
[src]

[src]

1.0.0
[src]

impl Clone for Normal
[src]

[src]

1.0.0
[src]

impl Clone for ChaChaRng
[src]

[src]

1.0.0
[src]

impl Clone for Exp
[src]

[src]

1.0.0
[src]

impl Clone for StandardNormal
[src]

[src]

1.0.0
[src]

impl Clone for LogNormal
[src]

[src]

1.0.0
[src]

impl Clone for XorShiftRng
[src]

[src]

1.0.0
[src]

impl Clone for Exp1
[src]

[src]

1.0.0
[src]

impl Clone for IsaacRng
[src]

[src]

1.0.0
[src]

impl Clone for _Unwind_Reason_Code

1.0.0
[src]

impl Clone for _Unwind_Action

1.0.0
[src]

impl<I> Clone for Utf16Encoder<I> where
    I: Clone, 
[src]

[src]

1.0.0
[src]

impl<T> Clone for NonZero<T> where
    T: Zeroable + Clone, 
[src]

[src]

1.0.0
[src]

impl Clone for timespec
[src]

[src]

1.0.0
[src]

impl Clone for timeval
[src]

[src]

1.0.0
[src]

impl Clone for utimbuf
[src]

[src]

1.0.0
[src]

impl Clone for stat
[src]

[src]

1.0.0
[src]

impl Clone for tm
[src]

[src]

1.0.0
[src]

Implementors