pub trait Clone: Sized {
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.
Implementors
impl Clone for core::cmp::Ordering
sourceimpl Clone for Infallible
1.34.0 (const: unstable) · sourceimpl Clone for FpCategory
sourceimpl Clone for IntErrorKind
1.55.0 · sourceimpl Clone for Which
sourceimpl Clone for SearchStep
sourceimpl Clone for core::sync::atomic::Ordering
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 Layout
1.28.0 · sourceimpl Clone for LayoutError
1.50.0 · sourceimpl Clone for TypeId
sourceimpl Clone for float64x1_t
1.59.0 · sourceimpl Clone for float64x1x2_t
1.59.0 · sourceimpl Clone for float64x1x3_t
1.59.0 · sourceimpl Clone for float64x1x4_t
1.59.0 · sourceimpl Clone for float64x2_t
1.59.0 · sourceimpl Clone for float64x2x2_t
1.59.0 · sourceimpl Clone for float64x2x3_t
1.59.0 · sourceimpl Clone for float64x2x4_t
1.59.0 · sourceimpl Clone for int16x2_t
sourceimpl Clone for uint16x2_t
sourceimpl Clone for float32x2_t
sourceimpl Clone for float32x2x2_t
sourceimpl Clone for float32x2x3_t
sourceimpl Clone for float32x2x4_t
sourceimpl Clone for float32x4_t
sourceimpl Clone for float32x4x2_t
sourceimpl Clone for float32x4x3_t
sourceimpl Clone for float32x4x4_t
sourceimpl Clone for int8x4_t
sourceimpl Clone for int8x8_t
sourceimpl Clone for int8x8x2_t
sourceimpl Clone for int8x8x3_t
sourceimpl Clone for int8x8x4_t
sourceimpl Clone for int8x16_t
sourceimpl Clone for int8x16x2_t
sourceimpl Clone for int8x16x3_t
sourceimpl Clone for int8x16x4_t
sourceimpl Clone for int16x4_t
sourceimpl Clone for int16x4x2_t
sourceimpl Clone for int16x4x3_t
sourceimpl Clone for int16x4x4_t
sourceimpl Clone for int16x8_t
sourceimpl Clone for int16x8x2_t
sourceimpl Clone for int16x8x3_t
sourceimpl Clone for int16x8x4_t
sourceimpl Clone for int32x2_t
sourceimpl Clone for int32x2x2_t
sourceimpl Clone for int32x2x3_t
sourceimpl Clone for int32x2x4_t
sourceimpl Clone for int32x4_t
sourceimpl Clone for int32x4x2_t
sourceimpl Clone for int32x4x3_t
sourceimpl Clone for int32x4x4_t
sourceimpl Clone for int64x1_t
sourceimpl Clone for int64x1x2_t
sourceimpl Clone for int64x1x3_t
sourceimpl Clone for int64x1x4_t
sourceimpl Clone for int64x2_t
sourceimpl Clone for int64x2x2_t
sourceimpl Clone for int64x2x3_t
sourceimpl Clone for int64x2x4_t
sourceimpl Clone for poly8x8_t
sourceimpl Clone for poly8x8x2_t
sourceimpl Clone for poly8x8x3_t
sourceimpl Clone for poly8x8x4_t
sourceimpl Clone for poly8x16_t
sourceimpl Clone for poly8x16x2_t
sourceimpl Clone for poly8x16x3_t
sourceimpl Clone for poly8x16x4_t
sourceimpl Clone for poly16x4_t
sourceimpl Clone for poly16x4x2_t
sourceimpl Clone for poly16x4x3_t
sourceimpl Clone for poly16x4x4_t
sourceimpl Clone for poly16x8_t
sourceimpl Clone for poly16x8x2_t
sourceimpl Clone for poly16x8x3_t
sourceimpl Clone for poly16x8x4_t
sourceimpl Clone for poly64x1_t
sourceimpl Clone for poly64x1x2_t
sourceimpl Clone for poly64x1x3_t
sourceimpl Clone for poly64x1x4_t
sourceimpl Clone for poly64x2_t
sourceimpl Clone for poly64x2x2_t
sourceimpl Clone for poly64x2x3_t
sourceimpl Clone for poly64x2x4_t
sourceimpl Clone for uint8x4_t
sourceimpl Clone for uint8x8_t
sourceimpl Clone for uint8x8x2_t
sourceimpl Clone for uint8x8x3_t
sourceimpl Clone for uint8x8x4_t
sourceimpl Clone for uint8x16_t
sourceimpl Clone for uint8x16x2_t
sourceimpl Clone for uint8x16x3_t
sourceimpl Clone for uint8x16x4_t
sourceimpl Clone for uint16x4_t
sourceimpl Clone for uint16x4x2_t
sourceimpl Clone for uint16x4x3_t
sourceimpl Clone for uint16x4x4_t
sourceimpl Clone for uint16x8_t
sourceimpl Clone for uint16x8x2_t
sourceimpl Clone for uint16x8x3_t
sourceimpl Clone for uint16x8x4_t
sourceimpl Clone for uint32x2_t
sourceimpl Clone for uint32x2x2_t
sourceimpl Clone for uint32x2x3_t
sourceimpl Clone for uint32x2x4_t
sourceimpl Clone for uint32x4_t
sourceimpl Clone for uint32x4x2_t
sourceimpl Clone for uint32x4x3_t
sourceimpl Clone for uint32x4x4_t
sourceimpl Clone for uint64x1_t
sourceimpl Clone for uint64x1x2_t
sourceimpl Clone for uint64x1x3_t
sourceimpl Clone for uint64x1x4_t
sourceimpl Clone for uint64x2_t
sourceimpl Clone for uint64x2x2_t
sourceimpl Clone for uint64x2x3_t
sourceimpl Clone for uint64x2x4_t
sourceimpl Clone for vector_bool_long
sourceimpl Clone for vector_double
sourceimpl Clone for vector_signed_long
sourceimpl Clone for vector_unsigned_long
sourceimpl Clone for v128
1.54.0 · sourcetarget_family="wasm" only.impl Clone for CpuidResult
1.27.0 · sourceimpl Clone for __m128
1.27.0 · sourceimpl Clone for __m128bh
sourceimpl Clone for __m128d
1.27.0 · sourceimpl Clone for __m128i
1.27.0 · sourceimpl Clone for __m256
1.27.0 · sourceimpl Clone for __m256bh
sourceimpl Clone for __m256d
1.27.0 · sourceimpl Clone for __m256i
1.27.0 · sourceimpl Clone for __m512
sourceimpl Clone for __m512bh
sourceimpl Clone for __m512d
sourceimpl Clone for __m512i
sourceimpl Clone for TryFromSliceError
1.34.0 · sourceimpl Clone for core::ascii::EscapeDefault
sourceimpl Clone for CharTryFromError
1.34.0 · sourceimpl Clone for DecodeUtf16Error
1.9.0 · sourceimpl Clone for core::char::EscapeDebug
1.20.0 · sourceimpl Clone for core::char::EscapeDefault
sourceimpl Clone for core::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 FromBytesUntilNulError
sourceimpl Clone for FromBytesWithNulError
sourceimpl Clone for Error
sourceimpl Clone for SipHasher
sourceimpl Clone for PhantomPinned
1.33.0 · 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 ParseBoolError
sourceimpl Clone for Utf8Error
sourceimpl Clone for RawWakerVTable
1.36.0 · sourceimpl Clone for Waker
1.36.0 · sourceimpl Clone for Duration
1.3.0 · sourceimpl Clone for FromFloatSecsError
sourceimpl<'a> Clone for Arguments<'a>
sourceimpl<'a> Clone for Location<'a>
1.10.0 · sourceimpl<'a> Clone for EscapeAscii<'a>
1.60.0 · sourceimpl<'a> Clone for CharSearcher<'a>
sourceimpl<'a> Clone for Bytes<'a>
sourceimpl<'a> Clone for CharIndices<'a>
sourceimpl<'a> Clone for Chars<'a>
sourceimpl<'a> Clone for EncodeUtf16<'a>
1.8.0 · sourceimpl<'a> Clone for core::str::EscapeDebug<'a>
1.34.0 · sourceimpl<'a> Clone for core::str::EscapeDefault<'a>
1.34.0 · sourceimpl<'a> Clone for core::str::EscapeUnicode<'a>
1.34.0 · sourceimpl<'a> Clone for Lines<'a>
sourceimpl<'a> Clone for LinesAny<'a>
sourceimpl<'a> Clone for SplitAsciiWhitespace<'a>
1.34.0 · sourceimpl<'a> Clone for SplitWhitespace<'a>
1.1.0 · sourceimpl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>
sourceimpl<'a, 'b> Clone for StrSearcher<'a, 'b>
sourceimpl<'a, 'b, const N: usize> Clone for CharArrayRefSearcher<'a, 'b, N>
sourceimpl<'a, F: Clone> Clone for CharPredicateSearcher<'a, F> where
F: FnMut(char) -> bool,
sourceimpl<'a, P> Clone for MatchIndices<'a, P> where
P: Pattern<'a, Searcher: Clone>,
1.5.0 · sourceimpl<'a, P> Clone for Matches<'a, P> where
P: Pattern<'a, Searcher: Clone>,
1.2.0 · sourceimpl<'a, P> Clone for RMatchIndices<'a, P> where
P: Pattern<'a, Searcher: Clone>,
1.5.0 · sourceimpl<'a, P> Clone for RMatches<'a, P> where
P: Pattern<'a, Searcher: Clone>,
1.2.0 · sourceimpl<'a, P> Clone for core::str::RSplit<'a, P> where
P: Pattern<'a, Searcher: Clone>,
sourceimpl<'a, P> Clone for RSplitN<'a, P> where
P: Pattern<'a, Searcher: Clone>,
sourceimpl<'a, P> Clone for RSplitTerminator<'a, P> where
P: Pattern<'a, Searcher: Clone>,
sourceimpl<'a, P> Clone for core::str::Split<'a, P> where
P: Pattern<'a, Searcher: Clone>,
sourceimpl<'a, P> Clone for SplitN<'a, P> where
P: Pattern<'a, Searcher: Clone>,
sourceimpl<'a, P> Clone for SplitTerminator<'a, P> where
P: Pattern<'a, Searcher: Clone>,
sourceimpl<'a, P: Pattern<'a, Searcher: Clone>> Clone for core::str::SplitInclusive<'a, P>
1.51.0 · sourceimpl<'a, T> Clone for RChunksExact<'a, T>
1.31.0 · sourceimpl<'a, T: Clone + 'a, const N: usize> Clone for ArrayWindows<'a, T, N>
sourceimpl<'a, const N: usize> Clone for CharArraySearcher<'a, N>
sourceimpl<'f> Clone for VaListImpl<'f>
sourceimpl<A> Clone for core::option::Iter<'_, A>
sourceimpl<A: Clone> Clone for Repeat<A>
sourceimpl<A: Clone> Clone for core::option::IntoIter<A>
sourceimpl<A: Clone, B: Clone> Clone for Chain<A, B>
sourceimpl<A: Clone, B: Clone> Clone for Zip<A, B>
sourceimpl<B: Clone, C: Clone> Clone for ControlFlow<B, C>
1.55.0 · sourceimpl<Dyn: ?Sized> Clone for DynMetadata<Dyn>
sourceimpl<F: Clone> Clone for FromFn<F>
1.34.0 · sourceimpl<F: Clone> Clone for OnceWith<F>
1.43.0 · sourceimpl<F: Clone> Clone for RepeatWith<F>
1.28.0 · sourceimpl<H> Clone for BuildHasherDefault<H>
1.7.0 · sourceimpl<I, G> Clone for IntersperseWith<I, G> where
I: Iterator + Clone,
I::Item: Clone,
G: Clone,
sourceimpl<I, U> Clone for Flatten<I> where
I: Clone + Iterator<Item: IntoIterator<IntoIter = U, Item = U::Item>>,
U: Clone + Iterator,
1.29.0 · sourceimpl<I: Clone + Iterator> Clone for Intersperse<I> where
I::Item: Clone,
I::Item: Clone,
sourceimpl<I: Clone + Iterator> Clone for Peekable<I> where
I::Item: Clone,
sourceimpl<I: Clone> Clone for FromIter<I>
sourceimpl<I: Clone> Clone for DecodeUtf16<I> where
I: Iterator<Item = u16>,
1.9.0 · sourceimpl<I: Clone> Clone for Cloned<I>
1.1.0 · sourceimpl<I: Clone> Clone for Copied<I>
1.36.0 · sourceimpl<I: Clone> Clone for Cycle<I>
sourceimpl<I: Clone> Clone for Enumerate<I>
sourceimpl<I: Clone> Clone for Fuse<I>
sourceimpl<I: Clone> Clone for Skip<I>
sourceimpl<I: Clone> Clone for StepBy<I>
1.28.0 · sourceimpl<I: Clone> Clone for Take<I>
sourceimpl<I: Clone, F: Clone> Clone for FilterMap<I, F>
sourceimpl<I: Clone, F: Clone> Clone for Inspect<I, F>
sourceimpl<I: Clone, F: Clone> Clone for Map<I, F>
sourceimpl<I: Clone, P: Clone> Clone for Filter<I, P>
sourceimpl<I: Clone, P: Clone> Clone for MapWhile<I, P>
1.57.0 · sourceimpl<I: Clone, P: Clone> Clone for SkipWhile<I, P>
sourceimpl<I: Clone, P: Clone> Clone for TakeWhile<I, P>
sourceimpl<I: Clone, St: Clone, F: Clone> Clone for Scan<I, St, F>
sourceimpl<I: Clone, U, F: Clone> Clone for FlatMap<I, U, F> where
U: Clone + IntoIterator<IntoIter: Clone>,
sourceimpl<Idx: Clone> Clone for Range<Idx>
sourceimpl<Idx: Clone> Clone for RangeFrom<Idx>
sourceimpl<Idx: Clone> Clone for RangeInclusive<Idx>
1.26.0 · sourceimpl<Idx: Clone> Clone for RangeTo<Idx>
sourceimpl<Idx: Clone> Clone for RangeToInclusive<Idx>
1.26.0 · sourceimpl<P: Clone> Clone for Pin<P>
1.33.0 · sourceimpl<T> Clone for Option<T> where
T: Clone,
const: unstable · sourceimpl<T> Clone for Pending<T>
1.48.0 · sourceimpl<T> Clone for Empty<T>
1.2.0 · sourceimpl<T> Clone for Discriminant<T>
1.21.0 · sourceimpl<T> Clone for core::result::Iter<'_, T>
sourceimpl<T> Clone for Chunks<'_, T>
sourceimpl<T> Clone for ChunksExact<'_, T>
1.31.0 · sourceimpl<T> Clone for core::slice::Iter<'_, T>
sourceimpl<T> Clone for RChunks<'_, T>
1.31.0 · sourceimpl<T> Clone for Windows<'_, T>
sourceimpl<T, E> Clone for Result<T, E> where
T: Clone,
E: Clone,
const: unstable · sourceimpl<T, P> Clone for core::slice::RSplit<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
1.27.0 · sourceimpl<T, P> Clone for core::slice::Split<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
sourceimpl<T, P> Clone for core::slice::SplitInclusive<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
1.51.0 · sourceimpl<T, const LANES: usize> Clone for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const LANES: usize> Clone for Simd<T, LANES> where
T: SimdElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const N: usize> Clone for ArrayChunks<'_, T, N>
sourceimpl<T: Copy> Clone for Cell<T>
sourceimpl<T: Copy> Clone for MaybeUninit<T>
1.36.0 · sourceimpl<T: Clone + ?Sized> Clone for ManuallyDrop<T>
1.20.0 · sourceimpl<T: Clone> Clone for Bound<T>
1.17.0 · sourceimpl<T: Clone> Clone for Poll<T>
1.36.0 · sourceimpl<T: Clone> Clone for RefCell<T>
sourceimpl<T: Clone> Clone for Reverse<T>
1.19.0 · sourceimpl<T: Clone> Clone for Ready<T>
1.48.0 · sourceimpl<T: Clone> Clone for Once<T>
1.2.0 · sourceimpl<T: Clone> Clone for Rev<T>
sourceimpl<T: Clone> Clone for OnceCell<T>
sourceimpl<T: Clone> Clone for Saturating<T>
sourceimpl<T: Clone> Clone for Wrapping<T>
sourceimpl<T: Clone> Clone for core::result::IntoIter<T>
sourceimpl<T: Clone, F: Clone> Clone for Successors<T, F>
1.34.0 · sourceimpl<T: Clone, const N: usize> Clone for [T; N]
1.58.0 · sourceimpl<T: Clone, const N: usize> Clone for core::array::IntoIter<T, N>
1.40.0 · sourceimpl<T: ?Sized> !Clone for &mut T
sourceShared references can be cloned, but mutable references cannot!
impl<T: ?Sized> Clone for *const T
const: unstable · sourceimpl<T: ?Sized> Clone for *mut T
const: unstable · sourceimpl<T: ?Sized> Clone for &T
const: unstable · sourceShared references can be cloned, but mutable references cannot!