Trait core::clone::Clone

1.0.0 · source · []
pub trait Clone: Sized {
    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,
}
Run

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

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)
  • 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());
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.

Implementors

This trait is implemented on arbitrary-length tuples.

Shared references can be cloned, but mutable references cannot!

Shared references can be cloned, but mutable references cannot!

impl Clone for Global

impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A>

impl Clone for Box<str>

impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>

impl<B: ?Sized + ToOwned> Clone for Cow<'_, B>

impl<T: Clone> Clone for BinaryHeap<T>

impl<T> Clone for Iter<'_, T>

impl<T: Clone> Clone for IntoIter<T>

impl<T: Clone> Clone for IntoIterSorted<T>

impl<K: Clone, V: Clone, A: Clone + Allocator> Clone for BTreeMap<K, V, A>

impl<K, V> Clone for Iter<'_, K, V>

impl<K, V> Clone for Keys<'_, K, V>

impl<K, V> Clone for Values<'_, K, V>

impl<K, V> Clone for Range<'_, K, V>

impl<T: Clone, A: Allocator + Clone> Clone for BTreeSet<T, A>

impl<T> Clone for Iter<'_, T>

impl<T> Clone for Range<'_, T>

impl<T, A: Allocator + Clone> Clone for Difference<'_, T, A>

impl<T> Clone for SymmetricDifference<'_, T>

impl<T, A: Allocator + Clone> Clone for Intersection<'_, T, A>

impl<T> Clone for Union<'_, T>

impl<T> Clone for Iter<'_, T>

impl<T: Clone> Clone for IntoIter<T>

impl<T> Clone for Cursor<'_, T>

impl<T: Clone> Clone for LinkedList<T>

impl<T: Clone, A: Clone + Allocator> Clone for IntoIter<T, A>

impl<T> Clone for Iter<'_, T>

impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A>

impl Clone for CString

impl Clone for NulError

impl Clone for Box<CStr>

impl<T: ?Sized> Clone for Rc<T>

impl<T: ?Sized> Clone for Weak<T>

impl Clone for String

impl<T: ?Sized> Clone for Arc<T>

impl<T: ?Sized> Clone for Weak<T>

impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A>

impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A>

impl Clone for ThreadId

impl Clone for Thread

impl<K, V, S> Clone for HashMap<K, V, S> where
    K: Clone,
    V: Clone,
    S: Clone

impl<K, V> Clone for Iter<'_, K, V>

impl<K, V> Clone for Keys<'_, K, V>

impl<K, V> Clone for Values<'_, K, V>

impl<T, S> Clone for HashSet<T, S> where
    T: Clone,
    S: Clone

impl<K> Clone for Iter<'_, K>

impl<T, S> Clone for Intersection<'_, T, S>

impl<T, S> Clone for Difference<'_, T, S>

impl<T, S> Clone for SymmetricDifference<'_, T, S>

impl<T, S> Clone for Union<'_, T, S>

impl Clone for VarError

impl<'a> Clone for Chain<'a>

impl Clone for OsString

impl Clone for Box<OsStr>

impl Clone for Metadata

impl Clone for FileType

impl<T> Clone for Cursor<T> where
    T: Clone

impl Clone for ErrorKind

impl Clone for Empty

impl Clone for Sink

impl<'a> Clone for IoSlice<'a>

impl Clone for SeekFrom

impl Clone for SocketAddr

impl Clone for IpAddr

impl Clone for Ipv4Addr

impl Clone for Ipv6Addr

impl Clone for Shutdown

impl Clone for SocketAddr

impl Clone for SocketCred

impl Clone for UCred

impl Clone for stat

impl<'handle> Clone for BorrowedHandle<'handle>

impl<'socket> Clone for BorrowedSocket<'socket>

impl<'fd> Clone for BorrowedFd<'fd>

impl<'a> Clone for Prefix<'a>

impl<'a> Clone for PrefixComponent<'a>

impl<'a> Clone for Component<'a>

impl<'a> Clone for Components<'a>

impl<'a> Clone for Iter<'a>

impl<'a> Clone for Ancestors<'a>

impl Clone for PathBuf

impl Clone for Box<Path>

impl Clone for Output

impl Clone for ExitStatus

impl Clone for ExitCode

impl<T: Clone> Clone for SendError<T>

impl Clone for RecvError

impl<T: Clone> Clone for TrySendError<T>

impl<T> Clone for Sender<T>

impl<T> Clone for SyncSender<T>

impl Clone for Instant

impl Clone for SystemTime

impl<T: Clone> Clone for SyncOnceCell<T>

impl<'a> Clone for EncodeWide<'a>

impl Clone for System

impl Clone for ()