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

Additional implementors

In addition to the implementors listed below, the following types also implement Clone:

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 TryFromSliceError
[src]

impl Clone for AllocErr
[src]

impl Clone for CannotReallocInPlace
[src]

impl Clone for LayoutErr
[src]

impl Clone for Layout
[src]

Implementors