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 Clone for SocketAddrV4
impl Clone for SocketAddrV4
fn clone(&self) -> SocketAddrV4
1.26.0 · sourceimpl Clone for AccessError
impl Clone for AccessError
fn clone(&self) -> AccessError
sourceimpl Clone for SocketAddr
impl Clone for SocketAddr
fn clone(&self) -> SocketAddr
sourceimpl<'fd> Clone for BorrowedFd<'fd>
impl<'fd> Clone for BorrowedFd<'fd>
fn clone(&self) -> BorrowedFd<'fd>
sourceimpl Clone for AddrParseError
impl Clone for AddrParseError
fn clone(&self) -> AddrParseError
1.7.0 · sourceimpl Clone for StripPrefixError
impl Clone for StripPrefixError
fn clone(&self) -> StripPrefixError
sourceimpl Clone for OpenOptions
impl Clone for OpenOptions
fn clone(&self) -> OpenOptions
1.10.0 · sourceimpl Clone for SocketAddr
impl Clone for SocketAddr
fn clone(&self) -> SocketAddr
sourceimpl Clone for Permissions
impl Clone for Permissions
fn clone(&self) -> Permissions
1.10.0 · sourceimpl Clone for FromBytesWithNulError
impl Clone for FromBytesWithNulError
fn clone(&self) -> FromBytesWithNulError
sourceimpl Clone for SocketCred
impl Clone for SocketCred
fn clone(&self) -> SocketCred
sourceimpl<T> Clone for Sender<T>
impl<T> Clone for Sender<T>
sourcefn clone(&self) -> Sender<T>
fn clone(&self) -> Sender<T>
Clone a sender to send to other threads.
Note, be aware of the lifetime of the sender because all senders
(including the original) need to be dropped in order for
Receiver::recv
to stop blocking.
sourceimpl<T> Clone for TrySendError<T> where
T: Clone,
impl<T> Clone for TrySendError<T> where
T: Clone,
fn clone(&self) -> TrySendError<T>
sourceimpl Clone for ExitStatusError
impl Clone for ExitStatusError
fn clone(&self) -> ExitStatusError
1.5.0 · sourceimpl Clone for WaitTimeoutResult
impl Clone for WaitTimeoutResult
fn clone(&self) -> WaitTimeoutResult
1.12.0 · sourceimpl Clone for RecvTimeoutError
impl Clone for RecvTimeoutError
fn clone(&self) -> RecvTimeoutError
1.58.0 · sourceimpl Clone for FromVecWithNulError
impl Clone for FromVecWithNulError
fn clone(&self) -> FromVecWithNulError
sourceimpl<T> Clone for SyncSender<T>
impl<T> Clone for SyncSender<T>
fn clone(&self) -> SyncSender<T>
1.8.0 · sourceimpl Clone for SystemTime
impl Clone for SystemTime
fn clone(&self) -> SystemTime
sourceimpl Clone for BacktraceStyle
impl Clone for BacktraceStyle
fn clone(&self) -> BacktraceStyle
1.7.0 · sourceimpl Clone for IntoStringError
impl Clone for IntoStringError
fn clone(&self) -> IntoStringError
sourceimpl Clone for TryRecvError
impl Clone for TryRecvError
fn clone(&self) -> TryRecvError
sourceimpl Clone for Ipv6MulticastScope
impl Clone for Ipv6MulticastScope
fn clone(&self) -> Ipv6MulticastScope
sourceimpl<'a> Clone for Components<'a>
impl<'a> Clone for Components<'a>
fn clone(&self) -> Components<'a>ⓘNotable traits for Components<'a>impl<'a> Iterator for Components<'a> type Item = Component<'a>;
sourceimpl<T> Clone for SyncOnceCell<T> where
T: Clone,
impl<T> Clone for SyncOnceCell<T> where
T: Clone,
fn clone(&self) -> SyncOnceCell<T>
sourceimpl Clone for ExitStatus
impl Clone for ExitStatus
fn clone(&self) -> ExitStatus
sourceimpl<'a> Clone for PrefixComponent<'a>
impl<'a> Clone for PrefixComponent<'a>
fn clone(&self) -> PrefixComponent<'a>
1.8.0 · sourceimpl Clone for SystemTimeError
impl Clone for SystemTimeError
fn clone(&self) -> SystemTimeError
sourceimpl Clone for SocketAddrV6
impl Clone for SocketAddrV6
fn clone(&self) -> SocketAddrV6
1.59.0 · sourceimpl Clone for TryFromCharError
impl Clone for TryFromCharError
fn clone(&self) -> TryFromCharError
1.33.0 · sourceimpl Clone for PhantomPinned
impl Clone for PhantomPinned
fn clone(&self) -> PhantomPinned
sourceimpl Clone for ToUppercase
impl Clone for ToUppercase
fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercaseimpl Iterator for ToUppercase type Item = char;
sourceimpl<'f> Clone for VaListImpl<'f>
impl<'f> Clone for VaListImpl<'f>
fn clone(&self) -> VaListImpl<'f>
1.34.0 · sourceimpl Clone for NonZeroI16
impl Clone for NonZeroI16
fn clone(&self) -> NonZeroI16
1.28.0 · sourceimpl Clone for NonZeroU64
impl Clone for NonZeroU64
fn clone(&self) -> NonZeroU64
1.28.0 · sourceimpl Clone for NonZeroUsize
impl Clone for NonZeroUsize
fn clone(&self) -> NonZeroUsize
impl<'_, T> !Clone for &'_ mut T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!
1.36.0 · sourceimpl Clone for RawWakerVTable
impl Clone for RawWakerVTable
fn clone(&self) -> RawWakerVTable
const: unstable · 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!
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,
1.34.0 · sourceimpl Clone for NonZeroI32
impl Clone for NonZeroI32
fn clone(&self) -> NonZeroI32
1.9.0 · sourceimpl Clone for DecodeUtf16Error
impl Clone for DecodeUtf16Error
fn clone(&self) -> DecodeUtf16Error
1.34.0 · sourceimpl Clone for NonZeroIsize
impl Clone for NonZeroIsize
fn clone(&self) -> NonZeroIsize
sourceimpl Clone for ParseIntError
impl Clone for ParseIntError
fn clone(&self) -> ParseIntError
sourceimpl<T> Clone for Saturating<T> where
T: Clone,
impl<T> Clone for Saturating<T> where
T: Clone,
fn clone(&self) -> Saturating<T>
sourceimpl Clone for FpCategory
impl Clone for FpCategory
fn clone(&self) -> FpCategory
1.28.0 · sourceimpl Clone for NonZeroU32
impl Clone for NonZeroU32
fn clone(&self) -> NonZeroU32
sourceimpl<Dyn> Clone for DynMetadata<Dyn> where
Dyn: ?Sized,
impl<Dyn> Clone for DynMetadata<Dyn> where
Dyn: ?Sized,
fn clone(&self) -> DynMetadata<Dyn>
sourceimpl Clone for ToLowercase
impl Clone for ToLowercase
fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercaseimpl Iterator for ToLowercase type Item = char;
1.20.0 · sourceimpl Clone for ParseCharError
impl Clone for ParseCharError
fn clone(&self) -> ParseCharError
sourceimpl Clone for EscapeDefault
impl Clone for EscapeDefault
fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = u8;
sourceimpl<T> Clone for PhantomData<T> where
T: ?Sized,
impl<T> Clone for PhantomData<T> where
T: ?Sized,
fn clone(&self) -> PhantomData<T>
1.28.0 · sourceimpl Clone for NonZeroU16
impl Clone for NonZeroU16
fn clone(&self) -> NonZeroU16
1.34.0 · sourceimpl Clone for NonZeroI64
impl Clone for NonZeroI64
fn clone(&self) -> NonZeroI64
1.55.0 · sourceimpl Clone for IntErrorKind
impl Clone for IntErrorKind
fn clone(&self) -> IntErrorKind
sourceimpl Clone for ParseFloatError
impl Clone for ParseFloatError
fn clone(&self) -> ParseFloatError
1.34.0 · sourceimpl Clone for TryFromSliceError
impl Clone for TryFromSliceError
fn clone(&self) -> TryFromSliceError
sourceimpl Clone for EscapeUnicode
impl Clone for EscapeUnicode
fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicodeimpl Iterator for EscapeUnicode type Item = char;
1.34.0 · sourceimpl Clone for CharTryFromError
impl Clone for CharTryFromError
fn clone(&self) -> CharTryFromError
1.20.0 · sourceimpl Clone for EscapeDebug
impl Clone for EscapeDebug
fn clone(&self) -> EscapeDebugⓘNotable traits for EscapeDebugimpl Iterator for EscapeDebug type Item = char;
1.34.0 · sourceimpl Clone for NonZeroI128
impl Clone for NonZeroI128
fn clone(&self) -> NonZeroI128
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>,
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>;