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 derived
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,
}RunHow 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 derived, 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
}
}RunAdditional 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
Clonethemselves. 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
impl Clone for CpuidResult
1.27.0 · source
impl Clone for CpuidResult
1.27.0 · sourcefn clone(&self) -> CpuidResult
sourceimpl Clone for FromBytesWithNulError
source
impl Clone for FromBytesWithNulError
sourcefn clone(&self) -> FromBytesWithNulError
sourceimpl Clone for FromBytesUntilNulError
source
impl Clone for FromBytesUntilNulError
sourcefn clone(&self) -> FromBytesUntilNulError
sourceimpl Clone for FromVecWithNulError
source
impl Clone for FromVecWithNulError
sourcefn clone(&self) -> FromVecWithNulError
sourceimpl Clone for IntoStringError
source
impl Clone for IntoStringError
sourcefn clone(&self) -> IntoStringError
sourceImplementors
impl Clone for std::cmp::Ordering
sourceimpl Clone for TryReserveErrorKind
sourceimpl Clone for Infallible
1.34.0 (const: unstable) · sourceimpl Clone for VarError
sourceimpl Clone for ErrorKind
sourceimpl Clone for SeekFrom
sourceimpl Clone for IpAddr
1.7.0 · sourceimpl Clone for Ipv6MulticastScope
sourceimpl Clone for Shutdown
sourceimpl Clone for std::net::SocketAddr
sourceimpl Clone for FpCategory
sourceimpl Clone for IntErrorKind
1.55.0 · sourceimpl Clone for BacktraceStyle
sourceimpl Clone for Which
sourceimpl Clone for SearchStep
sourceimpl Clone for std::sync::atomic::Ordering
sourceimpl Clone for RecvTimeoutError
1.12.0 · sourceimpl Clone for TryRecvError
sourceimpl Clone for bool
const: unstable · sourceimpl Clone for char
const: unstable · sourceimpl Clone for f32
const: unstable · sourceimpl Clone for f64
const: unstable · sourceimpl Clone for i8
const: unstable · sourceimpl Clone for i16
const: unstable · sourceimpl Clone for i32
const: unstable · sourceimpl Clone for i64
const: unstable · sourceimpl Clone for i128
const: unstable · sourceimpl Clone for isize
const: unstable · sourceimpl Clone for !
const: unstable · sourceimpl Clone for u8
const: unstable · sourceimpl Clone for u16
const: unstable · sourceimpl Clone for u32
const: unstable · sourceimpl Clone for u64
const: unstable · sourceimpl Clone for u128
const: unstable · sourceimpl Clone for usize
const: unstable · sourceimpl Clone for AllocError
sourceimpl Clone for Global
sourceimpl Clone for Layout
1.28.0 · sourceimpl Clone for LayoutError
1.50.0 · sourceimpl Clone for System
1.28.0 · sourceimpl Clone for TypeId
sourceimpl Clone for TryFromSliceError
1.34.0 · sourceimpl Clone for std::ascii::EscapeDefault
sourceimpl Clone for Box<str, Global>
1.3.0 · sourceimpl Clone for Box<CStr, Global>
1.29.0 · sourceimpl Clone for Box<OsStr>
1.29.0 · sourceimpl Clone for Box<Path>
1.29.0 · sourceimpl Clone for CharTryFromError
1.34.0 · sourceimpl Clone for DecodeUtf16Error
1.9.0 · sourceimpl Clone for std::char::EscapeDebug
1.20.0 · sourceimpl Clone for std::char::EscapeDefault
sourceimpl Clone for std::char::EscapeUnicode
sourceimpl Clone for ParseCharError
1.20.0 · sourceimpl Clone for ToLowercase
sourceimpl Clone for ToUppercase
sourceimpl Clone for TryFromCharError
1.59.0 · sourceimpl Clone for DefaultHasher
1.13.0 · sourceimpl Clone for RandomState
1.7.0 · sourceimpl Clone for TryReserveError
1.57.0 · sourceimpl Clone for OsString
sourceimpl Clone for Error
sourceimpl Clone for FileType
1.1.0 · sourceimpl Clone for Metadata
sourceimpl Clone for OpenOptions
sourceimpl Clone for Permissions
sourceimpl Clone for SipHasher
sourceimpl Clone for std::io::Empty
sourceimpl Clone for Sink
sourceimpl Clone for PhantomPinned
1.33.0 · sourceimpl Clone for AddrParseError
sourceimpl Clone for Ipv4Addr
sourceimpl Clone for Ipv6Addr
sourceimpl Clone for SocketAddrV4
sourceimpl Clone for SocketAddrV6
sourceimpl Clone for NonZeroI8
1.34.0 · sourceimpl Clone for NonZeroI16
1.34.0 · sourceimpl Clone for NonZeroI32
1.34.0 · sourceimpl Clone for NonZeroI64
1.34.0 · sourceimpl Clone for NonZeroI128
1.34.0 · sourceimpl Clone for NonZeroIsize
1.34.0 · sourceimpl Clone for NonZeroU8
1.28.0 · sourceimpl Clone for NonZeroU16
1.28.0 · sourceimpl Clone for NonZeroU32
1.28.0 · sourceimpl Clone for NonZeroU64
1.28.0 · sourceimpl Clone for NonZeroU128
1.28.0 · sourceimpl Clone for NonZeroUsize
1.28.0 · sourceimpl Clone for ParseFloatError
sourceimpl Clone for ParseIntError
sourceimpl Clone for TryFromIntError
1.34.0 · sourceimpl Clone for RangeFull
sourceimpl Clone for stat
1.1.0 · sourceimpl Clone for std::os::unix::net::SocketAddr
1.10.0 · sourceimpl Clone for SocketCred
sourceimpl Clone for UCred
sourceimpl Clone for InvalidHandleError
sourceimpl Clone for NullHandleError
sourceimpl Clone for PathBuf
sourceimpl Clone for StripPrefixError
1.7.0 · sourceimpl Clone for ExitCode
1.61.0 · sourceimpl Clone for ExitStatus
sourceimpl Clone for ExitStatusError
sourceimpl Clone for Output
sourceimpl Clone for ParseBoolError
sourceimpl Clone for Utf8Error
sourceimpl Clone for FromUtf8Error
sourceimpl Clone for String
sourceimpl Clone for RecvError
sourceimpl Clone for WaitTimeoutResult
1.5.0 · sourceimpl Clone for RawWakerVTable
1.36.0 · sourceimpl Clone for Waker
1.36.0 · sourceimpl Clone for AccessError
1.26.0 · sourceimpl Clone for Thread
sourceimpl Clone for ThreadId
1.19.0 · sourceimpl Clone for Duration
1.3.0 · sourceimpl Clone for FromFloatSecsError
sourceimpl Clone for Instant
1.8.0 · sourceimpl Clone for SystemTime
1.8.0 · sourceimpl Clone for SystemTimeError
1.8.0 · sourceimpl<'_, A> Clone for std::option::Iter<'_, A>
sourceimpl<'_, B> Clone for Cow<'_, B> where
B: ToOwned + ?Sized,
sourceimpl<'_, K, V> Clone for std::collections::btree_map::Iter<'_, K, V>
sourceimpl<'_, K, V> Clone for std::collections::btree_map::Keys<'_, K, V>
sourceimpl<'_, K, V> Clone for std::collections::btree_map::Range<'_, K, V>
1.17.0 · sourceimpl<'_, K, V> Clone for std::collections::btree_map::Values<'_, K, V>
sourceimpl<'_, T> !Clone for &'_ mut T where
T: ?Sized,
sourceShared references can be cloned, but mutable references cannot!
impl<'_, T> Clone for &'_ T where
T: ?Sized,
const: unstable · sourceShared references can be cloned, but mutable references cannot!