pub trait Clone {
    fn clone(&self) -> Self;

    fn clone_from(&mut self, source: &Self) { ... }
}
Expand description

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

Differs from Copy in that Copy is implicit and an inexpensive bit-wise copy, 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.

For a generic struct, #[derive] implements Clone conditionally by adding bound Clone on generic parameters.

// `derive` implements Clone for Reading<T> when T is Clone.
#[derive(Clone)]
struct Reading<T> {
    frequency: T,
}

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 a generic struct holding a function pointer. In this case, the implementation of Clone cannot be derived, but can be implemented as:

struct Generate<T>(fn() -> T);

impl<T> Copy for Generate<T> {}

impl<T> Clone for Generate<T> {
    fn clone(&self) -> Self {
        *self
    }
}

Additional implementors

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

  • Function item types (i.e., the distinct types defined for each function)
  • Function pointer types (e.g., fn() -> i32)
  • Tuple types, if each component also implements Clone (e.g., (), (i32, bool))
  • Closure types, if they capture no value from the environment or if all such captured values implement Clone themselves. Note that variables captured by shared reference always implement Clone (even if the referent doesn’t), while variables captured by mutable reference never implement Clone.

Required methods

Returns a copy of the value.

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

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

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

Clone a sender to send to other threads.

Note, be aware of the lifetime of the sender because all senders (including the original) need to be dropped in order for Receiver::recv to stop blocking.

Shared references can be cloned, but mutable references cannot!

Shared references can be cloned, but mutable references cannot!

Makes a clone of the Arc pointer.

This creates another pointer to the same allocation, increasing the strong reference count.

Examples
use std::sync::Arc;

let five = Arc::new(5);

let _ = Arc::clone(&five);

Makes a clone of the Weak pointer that points to the same allocation.

Examples
use std::sync::{Arc, Weak};

let weak_five = Arc::downgrade(&Arc::new(5));

let _ = Weak::clone(&weak_five);

Makes a clone of the Rc pointer.

This creates another pointer to the same allocation, increasing the strong reference count.

Examples
use std::rc::Rc;

let five = Rc::new(5);

let _ = Rc::clone(&five);

Makes a clone of the Weak pointer that points to the same allocation.

Examples
use std::rc::{Rc, Weak};

let weak_five = Rc::downgrade(&Rc::new(5));

let _ = Weak::clone(&weak_five);

Returns a new box with a clone() of this box’s contents.

Examples
let x = Box::new(5);
let y = x.clone();

// The value is the same
assert_eq!(x, y);

// But they are unique objects
assert_ne!(&*x as *const i32, &*y as *const i32);

Copies source’s contents into self without creating a new allocation.

Examples
let x = Box::new(5);
let mut y = Box::new(10);
let yp: *const i32 = &*y;

y.clone_from(&x);

// The value is the same
assert_eq!(x, y);

// And no allocation occurred
assert_eq!(yp, &*y);

Implementors