Trait std::clone::Clone 1.0.0[−][src]
#[lang = "clone"]pub trait Clone {#[must_use = "cloning is often expensive and is not expected to have side effects"]fn clone(&self) -> Self; fn clone_from(&mut self, source: &Self) { ... } }
A common trait for the ability to explicitly duplicate an object.
Differs from Copy
in that Copy
is implicit and extremely inexpensive, 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 derive
d
implementation of clone
calls clone
on each field.
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 an array holding more than 32 elements of a type that is Clone
; the standard
library only implements Clone
up until arrays of size 32. In this case, the implementation of
Clone
cannot be derive
d, but can be implemented as:
#[derive(Copy)] struct Stats { frequencies: [i32; 100], } impl Clone for Stats { fn clone(&self) -> Stats { *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
) - Array types, for all sizes, if the item type also implements
Clone
(e.g.[i32; 123456]
) - Tuple types, if each component also implements
Clone
(e.g.()
,(i32, bool)
) - 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 implementClone
(even if the referent doesn't), while variables captured by mutable reference never implementClone
.
Required Methods
#[must_use = "cloning is often expensive and is not expected to have side effects"]
fn clone(&self) -> Self
Provided Methods
fn clone_from(&mut self, source: &Self)
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.
Implementations on Foreign Types
impl Clone for TryFromSliceError
[src]
impl Clone for TryFromSliceError
fn clone(&self) -> TryFromSliceError
[src]
fn clone(&self) -> TryFromSliceError
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Implementors
impl<'a> Clone for SplitWhitespace<'a>
impl Clone for LocalWaker
impl Clone for Duration
impl<'a, P> Clone for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl Clone for TraitObject
impl Clone for Utf8Error
impl<'a, P> Clone for MatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl<A, B> Clone for Chain<A, B> where
A: Clone,
B: Clone,impl<T> Clone for *mut T where
T: ?Sized,impl Clone for char
impl Clone for u8
impl Clone for u32
impl Clone for TypeId
impl Clone for NonZeroU16
impl<Idx> Clone for RangeToInclusive<Idx> where
Idx: Clone,impl<T> Clone for Option<T> where
T: Clone,impl Clone for u64
impl<'a, P> Clone for RSplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl Clone for CharTryFromError
impl<'a> Clone for CharSearcher<'a>
impl<'a> Clone for Chars<'a>
impl Clone for ToLowercase
impl Clone for EscapeUnicode
impl<'a, P> Clone for RSplitN<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl<'a, T> Clone for Chunks<'a, T>
impl<'a, P> Clone for Matches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl<I> Clone for DecodeUtf16<I> where
I: Clone + Iterator<Item = u16>,impl<'a> Clone for EncodeUtf16<'a>
impl<'a, P> Clone for std::str::RSplit<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl Clone for NonZeroU128
impl Clone for std::sync::atomic::Ordering
impl<I, P> Clone for SkipWhile<I, P> where
I: Clone,
P: Clone,impl Clone for ParseFloatError
impl<Idx> Clone for RangeTo<Idx> where
Idx: Clone,impl<'a, P> Clone for SplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl<T> Clone for Poll<T> where
T: Clone,impl<I, U, F> Clone for FlatMap<I, U, F> where
F: Clone,
I: Clone,
U: Clone + IntoIterator,
<U as IntoIterator>::IntoIter: Clone,impl Clone for AllocErr
impl<'a, F> Clone for CharPredicateSearcher<'a, F> where
F: Clone + FnMut(char) -> bool,impl Clone for NoneError
impl<Idx> Clone for std::ops::Range<Idx> where
Idx: Clone,impl Clone for DecodeUtf16Error
impl<T> Clone for Reverse<T> where
T: Clone,impl Clone for FpCategory
impl Clone for i32
impl<I> Clone for Fuse<I> where
I: Clone,impl Clone for ToUppercase
impl Clone for u128
impl<'a, T> Clone for Windows<'a, T>
impl Clone for i16
impl Clone for LayoutErr
impl<'a> Clone for Bytes<'a>
impl<T> Clone for Bound<T> where
T: Clone,impl<T> Clone for ManuallyDrop<T> where
T: Clone,impl<T> Clone for RefCell<T> where
T: Clone,impl Clone for RangeFull
impl<I> Clone for Cycle<I> where
I: Clone,impl<'a, P> Clone for RMatches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl Clone for ParseCharError
impl<'a, T> Clone for ExactChunks<'a, T>
impl<'a> Clone for Arguments<'a>
impl<I, F> Clone for Map<I, F> where
F: Clone,
I: Clone,impl Clone for !
impl Clone for Waker
impl Clone for Error
impl Clone for i128
impl Clone for bool
impl<F> Clone for RepeatWith<F> where
F: Clone,impl<I> Clone for Peekable<I> where
I: Clone + Iterator,
<I as Iterator>::Item: Clone,impl Clone for Layout
impl Clone for isize
impl Clone for TryFromIntError
impl<I> Clone for Cloned<I> where
I: Clone,impl<T> Clone for Empty<T>
impl<I, F> Clone for Inspect<I, F> where
F: Clone,
I: Clone,impl<T> Clone for NonNull<T> where
T: ?Sized,impl<A, B> Clone for Zip<A, B> where
A: Clone,
B: Clone,impl<A> Clone for std::option::IntoIter<A> where
A: Clone,impl<Idx> Clone for RangeFrom<Idx> where
Idx: Clone,impl<I> Clone for Skip<I> where
I: Clone,impl<I, P> Clone for TakeWhile<I, P> where
I: Clone,
P: Clone,impl<'a> Clone for SplitAsciiWhitespace<'a>
impl<T, E> Clone for Result<T, E> where
E: Clone,
T: Clone,impl<I> Clone for StepBy<I> where
I: Clone,impl Clone for Pinned
impl<I, St, F> Clone for Scan<I, St, F> where
F: Clone,
I: Clone,
St: Clone,impl<I> Clone for Take<I> where
I: Clone,impl<T> Clone for PhantomData<T> where
T: ?Sized,impl<I, P> Clone for Filter<I, P> where
I: Clone,
P: Clone,impl Clone for i8
impl<I> Clone for Enumerate<I> where
I: Clone,impl<A> Clone for Repeat<A> where
A: Clone,impl Clone for CannotReallocInPlace
impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>
impl<T> Clone for Wrapping<T> where
T: Clone,impl<'a, T> Clone for std::slice::Iter<'a, T>
impl Clone for NonZeroU64
impl<'a> Clone for CharIndices<'a>
impl Clone for NonZeroUsize
impl<'a, A> Clone for std::option::Iter<'a, A>
impl<I> Clone for DecodeUtf8<I> where
I: Clone + Iterator<Item = u8>,impl<'a, P> Clone for std::str::Split<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl Clone for std::cmp::Ordering
impl<H> Clone for BuildHasherDefault<H>
impl<T> Clone for Once<T> where
T: Clone,impl Clone for SipHasher
impl<'a, P> Clone for SplitN<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,impl Clone for u16
impl<T> Clone for Rev<T> where
T: Clone,impl Clone for EscapeDefault
impl Clone for ParseIntError
impl<I, F> Clone for FilterMap<I, F> where
F: Clone,
I: Clone,impl Clone for i64
impl Clone for UnicodeVersion
impl<'a> Clone for Lines<'a>
impl Clone for EscapeDebug
impl<T> Clone for Discriminant<T>
impl<'a, T, P> Clone for std::slice::Split<'a, T, P> where
P: Clone + FnMut(&T) -> bool,impl<T> Clone for std::result::IntoIter<T> where
T: Clone,impl<T> Clone for Cell<T> where
T: Copy,impl<Idx> Clone for RangeInclusive<Idx> where
Idx: Clone,impl<'a, 'b> Clone for StrSearcher<'a, 'b>
impl Clone for NonZeroU32
impl Clone for ParseBoolError
impl<'a, T, P> Clone for std::slice::RSplit<'a, T, P> where
P: Clone + FnMut(&T) -> bool,
T: 'a + Clone,impl<Y, R> Clone for GeneratorState<Y, R> where
R: Clone,
Y: Clone,impl Clone for usize
impl Clone for f64
impl Clone for f32
impl<'a, T> Clone for std::result::Iter<'a, T>
impl<I, U> Clone for Flatten<I> where
I: Iterator + Clone,
U: Iterator + Clone,
<I as Iterator>::Item: IntoIterator,
<<I as Iterator>::Item as IntoIterator>::IntoIter == U,
<<I as Iterator>::Item as IntoIterator>::Item == <U as Iterator>::Item,impl<'a> Clone for LinesAny<'a>
impl Clone for NonZeroU8
impl<T> Clone for *const T where
T: ?Sized,impl<'a, T> Clone for &'a T where
T: ?Sized,impl Clone for SearchStep
impl<T> Clone for BTreeSet<T> where
T: Clone,impl<'a, T> Clone for std::collections::btree_set::Iter<'a, T>
impl<'a, T> Clone for std::collections::btree_set::Range<'a, T>
impl<K, V> Clone for BTreeMap<K, V> where
K: Clone,
V: Clone,impl Clone for ParseError
impl Clone for CollectionAllocErr
impl<'a, T> Clone for std::collections::binary_heap::Iter<'a, T>
impl<'a, K, V> Clone for std::collections::btree_map::Iter<'a, K, V>
impl Clone for String
impl<'a, T> Clone for std::collections::btree_set::Union<'a, T>
impl<T> Clone for Box<[T]> where
T: Clone,impl<T> Clone for BinaryHeap<T> where
T: Clone,impl<'a, K, V> Clone for std::collections::btree_map::Values<'a, K, V>
impl<'a, K, V> Clone for std::collections::btree_map::Range<'a, K, V>
impl<T> Clone for Vec<T> where
T: Clone,impl<'a, T> Clone for std::collections::btree_set::Intersection<'a, T>
impl<T> Clone for std::collections::vec_deque::IntoIter<T> where
T: Clone,impl<T> Clone for std::vec::IntoIter<T> where
T: Clone,impl<'a, T> Clone for std::collections::vec_deque::Iter<'a, T>
impl<T> Clone for Box<T> where
T: Clone,impl<'a, K, V> Clone for std::collections::btree_map::Keys<'a, K, V>
impl<'a, T> Clone for std::collections::btree_set::Difference<'a, T>
impl<'a, B> Clone for Cow<'a, B> where
B: ToOwned + ?Sized,impl Clone for Global
impl<T> Clone for Rc<T> where
T: ?Sized,impl Clone for Box<str>
impl<T> Clone for VecDeque<T> where
T: Clone,impl<T> Clone for std::sync::Weak<T> where
T: ?Sized,impl<T> Clone for std::collections::binary_heap::IntoIter<T> where
T: Clone,impl<T> Clone for std::collections::linked_list::IntoIter<T> where
T: Clone,impl<'a, T> Clone for std::collections::btree_set::SymmetricDifference<'a, T>
impl<T> Clone for LinkedList<T> where
T: Clone,impl<T> Clone for Arc<T> where
T: ?Sized,impl<'a, T> Clone for std::collections::linked_list::Iter<'a, T>
impl<T> Clone for std::rc::Weak<T> where
T: ?Sized,impl Clone for ThreadId
impl Clone for Thread
impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S>
impl<'a, K, V> Clone for std::collections::hash_map::Iter<'a, K, V>
impl<'a, K, V> Clone for std::collections::hash_map::Keys<'a, K, V>
impl<'a, K, V> Clone for std::collections::hash_map::Values<'a, K, V>
impl Clone for RandomState
impl Clone for DefaultHasher
impl<T: Clone, S: Clone> Clone for HashSet<T, S>
impl<'a, K> Clone for std::collections::hash_set::Iter<'a, K>
impl<'a, T, S> Clone for std::collections::hash_set::Intersection<'a, T, S>
impl<'a, T, S> Clone for std::collections::hash_set::Difference<'a, T, S>
impl<'a, T, S> Clone for std::collections::hash_set::SymmetricDifference<'a, T, S>
impl<'a, T, S> Clone for std::collections::hash_set::Union<'a, T, S>
impl Clone for VarError
impl Clone for CString
impl Clone for NulError
impl Clone for FromBytesWithNulError
impl Clone for IntoStringError
impl Clone for Box<CStr>
impl Clone for OsString
impl Clone for Box<OsStr>
impl Clone for Metadata
impl Clone for OpenOptions
impl Clone for Permissions
impl Clone for FileType
impl<T: Clone> Clone for Cursor<T>
impl Clone for ErrorKind
impl Clone for SeekFrom
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for Ipv4Addr
impl Clone for Ipv6Addr
impl Clone for std::net::SocketAddr
impl Clone for SocketAddrV4
impl Clone for SocketAddrV6
impl Clone for AddrParseError
impl Clone for Shutdown
impl Clone for stat
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 std::path::Iter<'a>
impl<'a> Clone for Ancestors<'a>
impl Clone for PathBuf
impl Clone for Box<Path>
impl Clone for StripPrefixError
impl Clone for Output
impl Clone for ExitStatus
impl Clone for ExitCode
impl<T: Clone> Clone for SendError<T>
impl Clone for RecvError
impl Clone for TryRecvError
impl Clone for RecvTimeoutError
impl<T: Clone> Clone for TrySendError<T>
impl<T> Clone for Sender<T>
impl<T> Clone for SyncSender<T>
impl Clone for WaitTimeoutResult
impl Clone for Instant
impl Clone for SystemTime
impl Clone for SystemTimeError
impl<'a> Clone for EncodeWide<'a>
impl Clone for std::os::unix::net::SocketAddr