clone
import std::core::clone; — in the prelude · source
Types
Clone
type trait Clone {
clone(&this) -> This;
}
Clone produces an independent copy: a heap-owning type allocates fresh storage and copies the contents; a POD type copies bitwise. It stays distinct from Copy so generic code can ask for the weaker promise - a Clone bound says "I can duplicate this if I ask", a Copy bound says "duplicating this is free and implicit".
The containers implement it parametrically: Array<T> is Clone when T is, and its clone is a deep copy into a fresh buffer. Rc and Arc deliberately do not implement Clone - their clone is an inherent method - so a T: Clone bound on a pointee is never satisfied by a cheap handle copy.
Methods
clone(&this) -> This;
Implementors
implement trait Clone for i8
implement trait Clone for i16
implement trait Clone for i32
implement trait Clone for i64
implement trait Clone for u8
implement trait Clone for u16
implement trait Clone for u32
implement trait Clone for u64
implement trait Clone for i128
implement trait Clone for u128
implement trait Clone for usize
implement trait Clone for isize
implement trait Clone for f32
implement trait Clone for f64
implement trait Clone for boolean
implement trait Clone for char
implement<T> trait Clone for struct Box<T, GlobalAlloc> where T: Clone
std::alloc::box
implement<T> trait Clone for struct Array<T, GlobalAlloc> where T: Clone
std::collections::array
implement<K, V> trait Clone for struct HashMap<K, V, GlobalAlloc> where K: Hash + Eq + Clone, V: Clone
std::collections::hashmap
implement<T> trait Clone for struct HashSet<T, GlobalAlloc> where T: Hash + Eq + Clone
std::collections::hashset
implement<A, B> trait Clone for struct Pair<A, B> where A: Clone, B: Clone
std::collections::pair
implement trait Clone for struct String<GlobalAlloc>
std::collections::string
implement<T> trait Clone for enum Option<T> where T: Clone
std::core::option
implement<T, E> trait Clone for enum Result<T, E> where T: Clone, E: Clone
std::core::result
string, the raw C-string view, has no Clone - there is nothing sensible to copy it into.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Clone {
clone(&this) -> This;
}Clone: explicit duplication.
Clone::clone(&this) -> This&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
ThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markertype struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype struct Rc<T, A = GlobalAlloc> where A: Allocator {
inner: RcInner<T, A>*;
}Rc<T, A>: single-threaded reference counting, with weak references.
std::alloc::rctype struct Arc<T, A = GlobalAlloc> where A: Allocator {
inner: ArcInner<T, A>*;
}Arc<T, A>: atomic reference counting, with weak references.
std::alloc::arcClone::clone(&this) -> Thisimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referenceprimitive usizePointer-width unsigned integer; the natural type for sizes and indices.
primitive typetype parameter Twhere T: Clone
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Box<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
alloc: A;
}Box<T, A>: single-owner heap allocation.
std::alloc::boxtype struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type parameter Kwhere K: Hash + Eq + Clone
type parameter Vwhere V: Clone
type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmaptype trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
type parameter Twhere T: Hash + Eq + Clone
type struct HashSet<T, A = GlobalAlloc> {
map: HashMap<T, (), A>;
}HashSet<T, A>: unordered collection of unique values.
std::collections::hashsettype parameter Awhere A: Clone
type parameter Bwhere B: Clone
type struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type parameter Ewhere E: Clone
primitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.