Clone in std::clone - Rust (original) (raw)

logo

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, whileClone 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 anythingCopy be Clone as well.

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,
}

Run

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
    }
}

Run

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

Returns a copy of the value.

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

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

Run

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.

impl Clone for stat

This is supported on Linux only.

impl Clone for std::os::unix:🥅:SocketAddr

This is supported on Unix only.

impl Clone for SocketCred

This is supported on (Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD) and Unix only.

impl Clone for UCred

This is supported on Unix only.

impl<'_, A> Clone for std::option::Iter<'_, A>

impl<'_, K, V> Clone for std::collections::btree_map::Iter<'_, K, V>

impl<'_, K, V> Clone for std::collections::btree_map::Keys<'_, K, V>

impl<'_, K, V> Clone for std::collections::btree_map::Range<'_, K, V>

impl<'_, K, V> Clone for std::collections::btree_map::Values<'_, K, V>

Shared references can be cloned, but mutable references cannot!

Shared references can be cloned, but mutable references cannot!

impl<'_, T> Clone for std::collections::binary_heap::Iter<'_, T>

impl<'_, T> Clone for std::collections::btree_set::Iter<'_, T>

impl<'_, T> Clone for std::collections::btree_set::Range<'_, T>

impl<'_, T> Clone for std::collections::btree_set::Union<'_, T>

impl<'_, T> Clone for std::collections::linked_list::Cursor<'_, T>

impl<'_, T> Clone for std::collections::linked_list::Iter<'_, T>

impl<'_, T> Clone for std::collections::vec_deque::Iter<'_, T>

impl<'_, T> Clone for std::result::Iter<'_, T>

impl<'_, T> Clone for std::slice::Iter<'_, T>

impl Clone for std::collections::hash_set::Iter<'_, K>

impl<K, V> Clone for std::collections::hash_map::Iter<'_, K, V>

impl<K, V> Clone for std::collections::hash_map::Keys<'_, K, V>

impl<K, V> Clone for std::collections::hash_map::Values<'_, K, V>

impl<T, S> Clone for std::collections::hash_set::Union<'_, T, S>