pub trait Clone {
fn clone(&self) -> Self;
fn clone_from(&mut self, source: &Self) { ... }
}
Expand description
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
) - 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
Provided methods
fn clone_from(&mut self, source: &Self)
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
sourceimpl<'_, T, S> Clone for Difference<'_, T, S>
impl<'_, T, S> Clone for Difference<'_, T, S>
pub fn clone(&self) -> Difference<'_, T, S>ⓘNotable traits for Difference<'a, T, S>impl<'a, T, S> Iterator for Difference<'a, T, S> where
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
sourceimpl<'_, T, S> Clone for Intersection<'_, T, S>
impl<'_, T, S> Clone for Intersection<'_, T, S>
pub fn clone(&self) -> Intersection<'_, T, S>ⓘNotable traits for Intersection<'a, T, S>impl<'a, T, S> Iterator for Intersection<'a, T, S> where
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
1.7.0 · sourceimpl Clone for RandomState
impl Clone for RandomState
pub fn clone(&self) -> RandomState
sourceimpl Clone for BacktraceStyle
impl Clone for BacktraceStyle
pub fn clone(&self) -> BacktraceStyle
1.13.0 · sourceimpl Clone for DefaultHasher
impl Clone for DefaultHasher
pub fn clone(&self) -> DefaultHasher
sourceimpl Clone for SocketAddr
impl Clone for SocketAddr
pub fn clone(&self) -> SocketAddr
sourceimpl Clone for AddrParseError
impl Clone for AddrParseError
pub fn clone(&self) -> AddrParseError
1.26.0 · sourceimpl Clone for AccessError
impl Clone for AccessError
pub fn clone(&self) -> AccessError
1.8.0 · sourceimpl Clone for SystemTimeError
impl Clone for SystemTimeError
pub fn clone(&self) -> SystemTimeError
sourceimpl Clone for OpenOptions
impl Clone for OpenOptions
pub fn clone(&self) -> OpenOptions
sourceimpl Clone for SocketCred
impl Clone for SocketCred
pub fn clone(&self) -> SocketCred
sourceimpl<'a> Clone for Components<'a>
impl<'a> Clone for Components<'a>
pub fn clone(&self) -> Components<'a>ⓘNotable traits for Components<'a>impl<'a> Iterator for Components<'a> type Item = Component<'a>;
sourceimpl<'a> Clone for PrefixComponent<'a>
impl<'a> Clone for PrefixComponent<'a>
pub fn clone(&self) -> PrefixComponent<'a>
sourceimpl Clone for Ipv6MulticastScope
impl Clone for Ipv6MulticastScope
pub fn clone(&self) -> Ipv6MulticastScope
1.7.0 · sourceimpl Clone for IntoStringError
impl Clone for IntoStringError
pub fn clone(&self) -> IntoStringError
sourceimpl<T> Clone for SyncOnceCell<T> where
T: Clone,
impl<T> Clone for SyncOnceCell<T> where
T: Clone,
pub fn clone(&self) -> SyncOnceCell<T>
sourceimpl<'fd> Clone for BorrowedFd<'fd>
impl<'fd> Clone for BorrowedFd<'fd>
pub fn clone(&self) -> BorrowedFd<'fd>
sourceimpl Clone for Permissions
impl Clone for Permissions
pub fn clone(&self) -> Permissions
sourceimpl Clone for SocketAddrV6
impl Clone for SocketAddrV6
pub fn clone(&self) -> SocketAddrV6
1.10.0 · sourceimpl Clone for SocketAddr
impl Clone for SocketAddr
pub fn clone(&self) -> SocketAddr
sourceimpl<'_, T, S> Clone for SymmetricDifference<'_, T, S>
impl<'_, T, S> Clone for SymmetricDifference<'_, T, S>
pub fn clone(&self) -> SymmetricDifference<'_, T, S>ⓘNotable traits for SymmetricDifference<'a, T, S>impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S> where
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
1.10.0 · sourceimpl Clone for FromBytesWithNulError
impl Clone for FromBytesWithNulError
pub fn clone(&self) -> FromBytesWithNulError
1.7.0 · sourceimpl Clone for StripPrefixError
impl Clone for StripPrefixError
pub fn clone(&self) -> StripPrefixError
1.58.0 · sourceimpl Clone for FromVecWithNulError
impl Clone for FromVecWithNulError
pub fn clone(&self) -> FromVecWithNulError
1.8.0 · sourceimpl Clone for SystemTime
impl Clone for SystemTime
pub fn clone(&self) -> SystemTime
sourceimpl Clone for SocketAddrV4
impl Clone for SocketAddrV4
pub fn clone(&self) -> SocketAddrV4
sourceimpl Clone for ExitStatusError
impl Clone for ExitStatusError
pub fn clone(&self) -> ExitStatusError
sourceimpl Clone for ExitStatus
impl Clone for ExitStatus
pub fn clone(&self) -> ExitStatus
sourceimpl<'f> Clone for VaListImpl<'f>
impl<'f> Clone for VaListImpl<'f>
pub fn clone(&self) -> VaListImpl<'f>
1.9.0 · sourceimpl<I> Clone for DecodeUtf16<I> where
I: Clone + Iterator<Item = u16>,
impl<I> Clone for DecodeUtf16<I> where
I: Clone + Iterator<Item = u16>,
pub fn clone(&self) -> DecodeUtf16<I>ⓘNotable traits for DecodeUtf16<I>impl<I> Iterator for DecodeUtf16<I> where
I: Iterator<Item = u16>, type Item = Result<char, DecodeUtf16Error>;
I: Iterator<Item = u16>, type Item = Result<char, DecodeUtf16Error>;
sourceimpl Clone for FromFloatSecsError
impl Clone for FromFloatSecsError
pub fn clone(&self) -> FromFloatSecsError
1.20.0 · sourceimpl Clone for EscapeDebug
impl Clone for EscapeDebug
pub fn clone(&self) -> EscapeDebugⓘNotable traits for EscapeDebugimpl Iterator for EscapeDebug type Item = char;
sourceimpl Clone for EscapeDefault
impl Clone for EscapeDefault
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = char;
sourceimpl<T, const LANES: usize> Clone for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Clone for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl Clone for EscapeUnicode
impl Clone for EscapeUnicode
pub fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicodeimpl Iterator for EscapeUnicode type Item = char;
sourceimpl Clone for ToUppercase
impl Clone for ToUppercase
pub fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercaseimpl Iterator for ToUppercase type Item = char;
impl<'_, T> !Clone for &'_ mut T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!
sourceimpl Clone for EscapeDefault
impl Clone for EscapeDefault
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = u8;
1.34.0 · sourceimpl Clone for CharTryFromError
impl Clone for CharTryFromError
pub fn clone(&self) -> CharTryFromError
1.9.0 · sourceimpl Clone for DecodeUtf16Error
impl Clone for DecodeUtf16Error
pub fn clone(&self) -> DecodeUtf16Error
1.59.0 · sourceimpl Clone for TryFromCharError
impl Clone for TryFromCharError
pub fn clone(&self) -> TryFromCharError
1.27.0 · sourceimpl Clone for CpuidResult
impl Clone for CpuidResult
pub fn clone(&self) -> CpuidResult
sourceimpl<'_, T> Clone for &'_ T where
T: ?Sized,
impl<'_, T> Clone for &'_ T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!
1.36.0 · sourceimpl Clone for RawWakerVTable
impl Clone for RawWakerVTable
pub fn clone(&self) -> RawWakerVTable
sourceimpl Clone for ToLowercase
impl Clone for ToLowercase
pub fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercaseimpl Iterator for ToLowercase type Item = char;
1.34.0 · sourceimpl Clone for TryFromSliceError
impl Clone for TryFromSliceError
pub fn clone(&self) -> TryFromSliceError
sourceimpl<T, const LANES: usize> Clone for Simd<T, LANES> where
T: SimdElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Clone for Simd<T, LANES> where
T: SimdElement,
LaneCount<LANES>: SupportedLaneCount,
1.20.0 · sourceimpl Clone for ParseCharError
impl Clone for ParseCharError
pub fn clone(&self) -> ParseCharError
sourceimpl Clone for FromUtf8Error
impl Clone for FromUtf8Error
pub fn clone(&self) -> FromUtf8Error
sourceimpl<T> Clone for LinkedList<T> where
T: Clone,
impl<T> Clone for LinkedList<T> where
T: Clone,
pub fn clone(&self) -> LinkedList<T>
pub fn clone_from(&mut self, other: &LinkedList<T>)
1.57.0 · sourceimpl Clone for TryReserveError
impl Clone for TryReserveError
pub fn clone(&self) -> TryReserveError
sourceimpl<T> Clone for IntoIterSorted<T> where
T: Clone,
impl<T> Clone for IntoIterSorted<T> where
T: Clone,
pub fn clone(&self) -> IntoIterSorted<T>ⓘNotable traits for IntoIterSorted<T>impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
T: Ord, type Item = T;