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: A value that can be duplicated:clonemakes an independent value equal toself, owning memory of its own whereselfowns some. - impl
CopyClone: The clone of acopytype: the value itself, allocating nothing. Install it withimpl Clone for Point = CopyClone;. - impl
DeriveClone: Derived clone: the same variant, each field cloned in declaration order. Install it in the type's module withimpl Clone for Entry = DeriveClone;; every field type must beClone.
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:
impl Clone for Unit = CopyClone;impl Clone for Bool = CopyClone;impl Clone for Int = CopyClone;impl Clone for u8 = CopyClone;impl Clone for u16 = CopyClone;impl Clone for u32 = CopyClone;impl Clone for u64 = CopyClone;impl Clone for usize = CopyClone;impl Clone for i8 = CopyClone;impl Clone for i16 = CopyClone;impl Clone for i32 = CopyClone;impl Clone for i64 = CopyClone;impl Clone for isize = CopyClone;impl Clone for f32 = CopyClone;impl Clone for f64 = CopyClone;impl Clone for Str = CopyClone;impl<T: Clone> Clone for Option<T>impl<T: Clone, E: Clone> Clone for Result<T, E>impl<T: Clone + forget> Clone for Box<T>
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.