Module std::clone

Duplicating values: Clone. A copy value is duplicated by naming it; clone is for the rest, a value that owns memory (String, Box<T>), whose clone owns fresh memory of its own. Allocating is an effect, so clone asks for Alloc, which serves the region self is in: the clone lives where the original does. Buf, Deque and HashMap have no Clone yet: a loop of element clones is refused (R-Loan, decision 4488).

Every program loads this module (through std::tuple), so x.clone() needs no use; a bound T: Clone does. Your own types implement it with a derive, impl Clone for Entry = DeriveClone; (each field cloned), or impl Clone for Point = CopyClone; for a copy type, or by writing clone.

Source: std/clone.weir; committed interface: std/clone.iface. Index

trait Clone

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

A value that can be duplicated: clone makes an independent value equal to self, owning memory of its own where self owns some.

Implementations:

method clone

fn clone(&self) -> Self with Alloc;

A duplicate of self, allocated by the Alloc serving self's region.

impl CopyClone

impl CopyClone<T: copy> Clone for T

The clone of a copy type: the value itself, allocating nothing. Install it with impl Clone for Point = CopyClone;.

impl DeriveClone

impl DeriveClone<T: reflect(Clone)> Clone for T

Derived clone: the same variant, each field cloned in declaration order. Install it in the type's module with impl Clone for Entry = DeriveClone;; every field type must be Clone.