Trait std::clone::Clone
pub trait Clone { 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 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 derive
d 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, }
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 derive
d, 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 } }
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
fn clone(&self) -> Self
Returns a copy of the value.
Examples
let hello = "Hello"; // &str implements Clone assert_eq!("Hello", hello.clone());
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 __m256bh
pub fn clone(&self) -> __m256bh
impl Clone for __m128i
pub fn clone(&self) -> __m128i
impl Clone for __m512bh
pub fn clone(&self) -> __m512bh
impl Clone for __m512d
pub fn clone(&self) -> __m512d
impl Clone for __m128d
pub fn clone(&self) -> __m128d
impl Clone for FromSecsError
pub fn clone(&self) -> FromSecsError
impl<'a> Clone for EscapeAscii<'a>
pub fn clone(&self) -> EscapeAscii<'a>
impl<'a> Iterator for EscapeAscii<'a> type Item = u8;
impl Clone for __m128
pub fn clone(&self) -> __m128
impl Clone for __m512i
pub fn clone(&self) -> __m512i
impl Clone for __m256d
pub fn clone(&self) -> __m256d
impl Clone for __m256
pub fn clone(&self) -> __m256
impl Clone for __m512
pub fn clone(&self) -> __m512
impl Clone for CpuidResult
pub fn clone(&self) -> CpuidResult
impl Clone for __m128bh
pub fn clone(&self) -> __m128bh
impl Clone for __m256i
pub fn clone(&self) -> __m256i
pub fn clone(&self) -> SplitInclusive<'_, T, P>
impl<'a, T, P> Iterator for SplitInclusive<'a, T, P> where P: FnMut(&T) -> bool, type Item = &'a [T];
Implementors
impl Clone for std::cmp::Ordering
impl Clone for TryReserveErrorKind
impl Clone for Infallible
impl Clone for VarError
impl Clone for ErrorKind
impl Clone for SeekFrom
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for Shutdown
impl Clone for std::net::SocketAddr
impl Clone for FpCategory
impl Clone for IntErrorKind
impl Clone for SearchStep
impl Clone for std::sync::atomic::Ordering
impl Clone for RecvTimeoutError
impl Clone for TryRecvError
impl Clone for bool
impl Clone for char
impl Clone for f32
impl Clone for f64
impl Clone for i8
impl Clone for i16
impl Clone for i32
impl Clone for i64
impl Clone for i128
impl Clone for isize
impl Clone for !
impl Clone for u8
impl Clone for u16
impl Clone for u32
impl Clone for u64
impl Clone for u128
impl Clone for usize
impl Clone for AllocError
impl Clone for Global
impl Clone for Layout
impl Clone for LayoutError
impl Clone for System
impl Clone for TypeId
impl Clone for TryFromSliceError
impl Clone for std::ascii::EscapeDefault
impl Clone for Box<str, Global>
impl Clone for Box<CStr>
impl Clone for Box<OsStr>
impl Clone for Box<Path>
impl Clone for CharTryFromError
impl Clone for DecodeUtf16Error
impl Clone for std::char::EscapeDebug
impl Clone for std::char::EscapeDefault
impl Clone for std::char::EscapeUnicode
impl Clone for ParseCharError
impl Clone for ToLowercase
impl Clone for ToUppercase
impl Clone for DefaultHasher
impl Clone for RandomState
impl Clone for TryReserveError
impl Clone for CString
impl Clone for FromBytesWithNulError
impl Clone for FromVecWithNulError
impl Clone for IntoStringError
impl Clone for NulError
impl Clone for OsString
impl Clone for Error
impl Clone for FileType
impl Clone for Metadata
impl Clone for OpenOptions
impl Clone for Permissions
impl Clone for SipHasher
impl Clone for std::io::Empty
impl Clone for Sink
impl Clone for PhantomPinned
impl Clone for AddrParseError
impl Clone for Ipv4Addr
impl Clone for Ipv6Addr
impl Clone for SocketAddrV4
impl Clone for SocketAddrV6
impl Clone for NonZeroI8
impl Clone for NonZeroI16
impl Clone for NonZeroI32
impl Clone for NonZeroI64
impl Clone for NonZeroI128
impl Clone for NonZeroIsize
impl Clone for NonZeroU8
impl Clone for NonZeroU16
impl Clone for NonZeroU32
impl Clone for NonZeroU64
impl Clone for NonZeroU128
impl Clone for NonZeroUsize
impl Clone for ParseFloatError
impl Clone for ParseIntError
impl Clone for TryFromIntError
impl Clone for RangeFull
impl Clone for SocketCred
impl Clone for PathBuf
impl Clone for StripPrefixError
impl Clone for ExitCode
impl Clone for ExitStatus
impl Clone for ExitStatusError
impl Clone for Output
impl Clone for ParseBoolError
impl Clone for Utf8Error
impl Clone for FromUtf8Error
impl Clone for String
impl Clone for RecvError
impl Clone for WaitTimeoutResult
impl Clone for RawWakerVTable
impl Clone for Waker
impl Clone for AccessError
impl Clone for Thread
impl Clone for ThreadId
impl Clone for Duration
impl Clone for Instant
impl Clone for SystemTime
impl Clone for SystemTimeError
impl<'_, A> Clone for std::option::Iter<'_, A>
impl<'_, K, V> Clone for std::collections::btree_map::Iter<'_, K, V>
impl<'_, K, V> Clone for std::collections::btree_map::Keys<'_, K, V>
impl<'_, K, V> Clone for std::collections::btree_map::Range<'_, K, V>
impl<'_, K, V> Clone for std::collections::btree_map::Values<'_, K, V>
Shared references can be cloned, but mutable references cannot!
Shared references can be cloned, but mutable references cannot!
impl<'_, T> Clone for std::collections::binary_heap::Iter<'_, T>
impl<'_, T> Clone for std::collections::btree_set::Difference<'_, T>
impl<'_, T> Clone for std::collections::btree_set::Intersection<'_, T>
impl<'_, T> Clone for std::collections::btree_set::Iter<'_, T>
impl<'_, T> Clone for std::collections::btree_set::Range<'_, T>
impl<'_, T> Clone for std::collections::btree_set::SymmetricDifference<'_, T>
impl<'_, T> Clone for std::collections::btree_set::Union<'_, T>
impl<'_, T> Clone for std::collections::linked_list::Cursor<'_, T>
impl<'_, T> Clone for std::collections::linked_list::Iter<'_, T>
impl<'_, T> Clone for std::collections::vec_deque::Iter<'_, T>
impl<'_, T> Clone for std::result::Iter<'_, T>
impl<'_, T> Clone for Chunks<'_, T>
impl<'_, T> Clone for ChunksExact<'_, T>
impl<'_, T> Clone for std::slice::Iter<'_, T>
impl<'_, T> Clone for RChunks<'_, T>
impl<'_, T> Clone for Windows<'_, T>
impl<'_, T, const N: usize> Clone for ArrayChunks<'_, T, N>
impl<'a> Clone for Component<'a>
impl<'a> Clone for Prefix<'a>
impl<'a> Clone for std::error::Chain<'a>
impl<'a> Clone for Arguments<'a>
impl<'a> Clone for IoSlice<'a>
impl<'a> Clone for EncodeWide<'a>
impl<'a> Clone for Location<'a>
impl<'a> Clone for Ancestors<'a>
impl<'a> Clone for Components<'a>
impl<'a> Clone for std::path::Iter<'a>
impl<'a> Clone for PrefixComponent<'a>
impl<'a> Clone for CharSearcher<'a>
impl<'a> Clone for Bytes<'a>
impl<'a> Clone for CharIndices<'a>
impl<'a> Clone for Chars<'a>
impl<'a> Clone for EncodeUtf16<'a>
impl<'a> Clone for std::str::EscapeDebug<'a>
impl<'a> Clone for std::str::EscapeDefault<'a>
impl<'a> Clone for std::str::EscapeUnicode<'a>
impl<'a> Clone for Lines<'a>
impl<'a> Clone for LinesAny<'a>
impl<'a> Clone for SplitAsciiWhitespace<'a>
impl<'a> Clone for SplitWhitespace<'a>
impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>
impl<'a, 'b> Clone for StrSearcher<'a, 'b>
impl<'a, P> Clone for MatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for Matches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RMatches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for std::str::RSplit<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RSplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for std::str::Split<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for std::str::SplitInclusive<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for SplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, T> Clone for RChunksExact<'a, T>
impl<'a, T, P> Clone for std::slice::RSplit<'a, T, P> where
T: 'a + Clone,
P: Clone + FnMut(&T) -> bool,
impl<'f> Clone for VaListImpl<'f>
impl<'fd> Clone for BorrowedFd<'fd>
impl<H> Clone for BuildHasherDefault<H>
impl<I> Clone for Intersperse<I> where
I: Clone + Iterator,
<I as Iterator>::Item: Clone,
<I as Iterator>::Item: Clone,
impl<I, G> Clone for IntersperseWith<I, G> where
I: Iterator + Clone,
G: Clone,
<I as Iterator>::Item: Clone,
impl<I, U> Clone for Flatten<I> where
I: Clone + Iterator,
U: Clone + Iterator,
<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<I, U, F> Clone for FlatMap<I, U, F> where
F: Clone,
I: Clone,
U: Clone + IntoIterator,
<U as IntoIterator>::IntoIter: Clone,
impl<K> Clone for std::collections::hash_set::Iter<'_, K>
impl<K, V> Clone for std::collections::hash_map::Iter<'_, K, V>
impl<K, V> Clone for std::collections::hash_map::Keys<'_, K, V>
impl<K, V> Clone for std::collections::hash_map::Values<'_, K, V>
impl<T> Clone for Pending<T>
impl<T> Clone for std::iter::Empty<T>
impl<T> Clone for Discriminant<T>
impl<T> Clone for Sender<T>
impl<T> Clone for SyncSender<T>
impl<T, A> Clone for std::collections::vec_deque::IntoIter<T, A> where
T: Clone,
A: Clone + Allocator,
impl<T, S> Clone for std::collections::hash_set::Difference<'_, T, S>
impl<T, S> Clone for std::collections::hash_set::Intersection<'_, T, S>
impl<T, S> Clone for std::collections::hash_set::SymmetricDifference<'_, T, S>
impl<T, S> Clone for std::collections::hash_set::Union<'_, T, S>
impl<T: Clone> Clone for TrySendError<T>
impl<T: Clone> Clone for SyncOnceCell<T>
impl<T: Clone> Clone for SendError<T>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/clone/trait.Clone.html