pub trait Clone: Sized {
// Required method
fn clone(&self) -> Self;
// Provided method
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,
}
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 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
}
}
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
) - 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§
sourcefn 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 AsciiChar
impl Clone for CharErrorKind
impl Clone for CaseMappingIter
impl Clone for EscapeDebugInner
impl Clone for core::cmp::Ordering
impl Clone for Infallible
impl Clone for Which
impl Clone for FromBytesWithNulErrorKind
impl Clone for core::fmt::Alignment
impl Clone for core::fmt::rt::Alignment
impl Clone for Count
impl Clone for Flag
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for AddrKind
impl Clone for SocketAddr
impl Clone for FloatErrorKind
impl Clone for FpCategory
impl Clone for IntErrorKind
impl Clone for FullDecoded
impl Clone for Sign
impl Clone for AlignmentEnum16
impl Clone for AlignmentEnum32
impl Clone for AlignmentEnum64
impl Clone for SearchStep
impl Clone for StrSearcherImpl
impl Clone for core::sync::atomic::Ordering
impl Clone for TryFromFloatSecsErrorKind
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 ()
impl Clone for usize
impl Clone for Layout
impl Clone for LayoutError
impl Clone for AllocError
impl Clone for TypeId
impl Clone for TryFromSliceError
impl Clone for core::ascii::EscapeDefault
impl Clone for BorrowRef<'_>
impl Clone for CharTryFromError
impl Clone for ParseCharError
impl Clone for DecodeUtf16Error
impl Clone for core::char::EscapeDebug
impl Clone for core::char::EscapeDefault
impl Clone for core::char::EscapeUnicode
impl Clone for ToLowercase
impl Clone for ToUppercase
impl Clone for TryFromCharError
impl Clone for float64x1_t
impl Clone for float64x1x2_t
impl Clone for float64x1x3_t
impl Clone for float64x1x4_t
impl Clone for float64x2_t
impl Clone for float64x2x2_t
impl Clone for float64x2x3_t
impl Clone for float64x2x4_t
impl Clone for int16x2_t
impl Clone for uint16x2_t
impl Clone for int8x4_t
impl Clone for uint8x4_t
impl Clone for float32x2_t
impl Clone for float32x2x2_t
impl Clone for float32x2x3_t
impl Clone for float32x2x4_t
impl Clone for float32x4_t
impl Clone for float32x4x2_t
impl Clone for float32x4x3_t
impl Clone for float32x4x4_t
impl Clone for int8x8_t
impl Clone for int8x8x2_t
impl Clone for int8x8x3_t
impl Clone for int8x8x4_t
impl Clone for int8x16_t
impl Clone for int8x16x2_t
impl Clone for int8x16x3_t
impl Clone for int8x16x4_t
impl Clone for int16x4_t
impl Clone for int16x4x2_t
impl Clone for int16x4x3_t
impl Clone for int16x4x4_t
impl Clone for int16x8_t
impl Clone for int16x8x2_t
impl Clone for int16x8x3_t
impl Clone for int16x8x4_t
impl Clone for int32x2_t
impl Clone for int32x2x2_t
impl Clone for int32x2x3_t
impl Clone for int32x2x4_t
impl Clone for int32x4_t
impl Clone for int32x4x2_t
impl Clone for int32x4x3_t
impl Clone for int32x4x4_t
impl Clone for int64x1_t
impl Clone for int64x1x2_t
impl Clone for int64x1x3_t
impl Clone for int64x1x4_t
impl Clone for int64x2_t
impl Clone for int64x2x2_t
impl Clone for int64x2x3_t
impl Clone for int64x2x4_t
impl Clone for poly8x8_t
impl Clone for poly8x8x2_t
impl Clone for poly8x8x3_t
impl Clone for poly8x8x4_t
impl Clone for poly8x16_t
impl Clone for poly8x16x2_t
impl Clone for poly8x16x3_t
impl Clone for poly8x16x4_t
impl Clone for poly16x4_t
impl Clone for poly16x4x2_t
impl Clone for poly16x4x3_t
impl Clone for poly16x4x4_t
impl Clone for poly16x8_t
impl Clone for poly16x8x2_t
impl Clone for poly16x8x3_t
impl Clone for poly16x8x4_t
impl Clone for poly64x1_t
impl Clone for poly64x1x2_t
impl Clone for poly64x1x3_t
impl Clone for poly64x1x4_t
impl Clone for poly64x2_t
impl Clone for poly64x2x2_t
impl Clone for poly64x2x3_t
impl Clone for poly64x2x4_t
impl Clone for uint8x8_t
impl Clone for uint8x8x2_t
impl Clone for uint8x8x3_t
impl Clone for uint8x8x4_t
impl Clone for uint8x16_t
impl Clone for uint8x16x2_t
impl Clone for uint8x16x3_t
impl Clone for uint8x16x4_t
impl Clone for uint16x4_t
impl Clone for uint16x4x2_t
impl Clone for uint16x4x3_t
impl Clone for uint16x4x4_t
impl Clone for uint16x8_t
impl Clone for uint16x8x2_t
impl Clone for uint16x8x3_t
impl Clone for uint16x8x4_t
impl Clone for uint32x2_t
impl Clone for uint32x2x2_t
impl Clone for uint32x2x3_t
impl Clone for uint32x2x4_t
impl Clone for uint32x4_t
impl Clone for uint32x4x2_t
impl Clone for uint32x4x3_t
impl Clone for uint32x4x4_t
impl Clone for uint64x1_t
impl Clone for uint64x1x2_t
impl Clone for uint64x1x3_t
impl Clone for uint64x1x4_t
impl Clone for uint64x2_t
impl Clone for uint64x2x2_t
impl Clone for uint64x2x3_t
impl Clone for uint64x2x4_t
impl Clone for vector_bool_char
impl Clone for vector_bool_int
impl Clone for vector_bool_short
impl Clone for vector_float
impl Clone for vector_signed_char
impl Clone for vector_signed_int
impl Clone for vector_signed_short
impl Clone for vector_unsigned_char
impl Clone for vector_unsigned_int
impl Clone for vector_unsigned_short
impl Clone for vector_bool_long
impl Clone for vector_double
impl Clone for vector_signed_long
impl Clone for vector_unsigned_long
impl Clone for f32x2
impl Clone for f32x4
impl Clone for f32x8
impl Clone for f32x16
impl Clone for f64x1
impl Clone for f64x2
impl Clone for f64x4
impl Clone for f64x8
impl Clone for i8x2
impl Clone for i8x4
impl Clone for i8x8
impl Clone for i8x16
impl Clone for i8x32
impl Clone for i8x64
impl Clone for i16x2
impl Clone for i16x4
impl Clone for i16x8
impl Clone for i16x16
impl Clone for i16x32
impl Clone for i32x2
impl Clone for i32x4
impl Clone for i32x8
impl Clone for i32x16
impl Clone for i64x1
impl Clone for i64x2
impl Clone for i64x4
impl Clone for i64x8
impl Clone for m8x16
impl Clone for m16x8
impl Clone for m32x4
impl Clone for m64x2
impl Clone for u8x2
impl Clone for u8x4
impl Clone for u8x8
impl Clone for u8x16
impl Clone for u8x32
impl Clone for u8x64
impl Clone for u16x2
impl Clone for u16x4
impl Clone for u16x8
impl Clone for u16x16
impl Clone for u16x32
impl Clone for u32x2
impl Clone for u32x4
impl Clone for u32x8
impl Clone for u32x16
impl Clone for u64x1
impl Clone for u64x2
impl Clone for u64x4
impl Clone for u64x8
impl Clone for v128
target_family="wasm"
only.impl Clone for CpuidResult
impl Clone for __m128
impl Clone for __m128bh
impl Clone for __m128d
impl Clone for __m128i
impl Clone for __m256
impl Clone for __m256bh
impl Clone for __m256d
impl Clone for __m256i
impl Clone for __m512
impl Clone for __m512bh
impl Clone for __m512d
impl Clone for __m512i
impl Clone for FromBytesUntilNulError
impl Clone for FromBytesWithNulError
impl Clone for Binary
impl Clone for LowerHex
impl Clone for Octal
impl Clone for UpperHex
impl Clone for Placeholder
impl Clone for Error
impl Clone for Sip13Rounds
impl Clone for Sip24Rounds
impl Clone for SipHasher24
impl Clone for SipHasher
impl Clone for State
impl Clone for PhantomPinned
impl Clone for Assume
impl Clone for Ipv4Addr
impl Clone for Ipv6Addr
impl Clone for AddrParseError
impl Clone for SocketAddrV4
impl Clone for SocketAddrV6
impl Clone for Big32x40
impl Clone for Big8x3
impl Clone for BiasedFp
impl Clone for Decimal
impl Clone for Number
impl Clone for ParseIntError
impl Clone for TryFromIntError
impl Clone for Decoded
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 IndexRange
impl Clone for RangeFull
impl Clone for core::ptr::alignment::Alignment
impl Clone for EscapeByte
impl Clone for TimSortRun
impl Clone for ParseBoolError
impl Clone for Utf8Error
impl Clone for EmptyNeedle
impl Clone for TwoWaySearcher
impl Clone for BytesIsNotEmpty
impl Clone for CharEscapeDebugContinue
impl Clone for CharEscapeDefault
impl Clone for CharEscapeUnicode
impl Clone for IsAsciiWhitespace
impl Clone for IsNotEmpty
impl Clone for IsWhitespace
impl Clone for LinesMap
impl Clone for UnsafeBytesToStr
impl Clone for RawWakerVTable
impl Clone for Waker
impl Clone for Duration
impl Clone for Nanoseconds
impl Clone for TryFromFloatSecsError
impl Clone for Span
impl<'a> Clone for Part<'a>
impl<'a> Clone for Source<'a>
impl<'a> Clone for Argument<'a>
impl<'a> Clone for Arguments<'a>
impl<'a> Clone for Formatted<'a>
impl<'a> Clone for Location<'a>
impl<'a> Clone for EscapeAscii<'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 core::str::iter::EscapeDebug<'a>
impl<'a> Clone for core::str::iter::EscapeDefault<'a>
impl<'a> Clone for core::str::iter::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> Clone for Utf8Chunk<'a>
impl<'a> Clone for Utf8Chunks<'a>
impl<'a> Clone for CharSearcher<'a>
impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>
impl<'a, 'b> Clone for StrSearcher<'a, 'b>
impl<'a, 'b, const N: usize> Clone for CharArrayRefSearcher<'a, 'b, N>
impl<'a, C: Clone + MultiCharEq> Clone for MultiCharEqSearcher<'a, C>
impl<'a, F> Clone for CharPredicateSearcher<'a, F>where F: FnMut(char) -> bool + Clone,
impl<'a, P> Clone for MatchIndices<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for MatchIndicesInternal<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for Matches<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for MatchesInternal<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for RMatchIndices<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for RMatches<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for core::str::iter::RSplit<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for RSplitN<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for RSplitTerminator<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for core::str::iter::Split<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for SplitInternal<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for SplitN<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for SplitNInternal<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P> Clone for SplitTerminator<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P: Pattern<'a, Searcher: Clone>> Clone for core::str::iter::SplitInclusive<'a, P>
impl<'a, T> Clone for RChunksExact<'a, T>
impl<'a, T: Clone + 'a, const N: usize> Clone for ArrayWindows<'a, T, N>
impl<'a, const N: usize> Clone for CharArraySearcher<'a, N>
impl<'f> Clone for VaListImpl<'f>
impl<A> Clone for core::option::Iter<'_, A>
impl<A: Clone> Clone for Repeat<A>
impl<A: Clone> Clone for core::option::IntoIter<A>
impl<A: Clone> Clone for Item<A>
impl<A: Clone, B: Clone> Clone for Chain<A, B>
impl<A: Clone, B: Clone> Clone for Zip<A, B>
impl<B: Clone, C: Clone> Clone for ControlFlow<B, C>
impl<Dyn: ?Sized> Clone for DynMetadata<Dyn>
impl<F: Clone> Clone for FromFn<F>
impl<F: Clone> Clone for OnceWith<F>
impl<F: Clone> Clone for RepeatWith<F>
impl<G: Clone> Clone for FromGenerator<G>
impl<H> Clone for BuildHasherDefault<H>
impl<I> Clone for DecodeUtf16<I>where I: Iterator<Item = u16> + Clone,
impl<I, G> Clone for IntersperseWith<I, G>where I: Iterator + Clone, I::Item: Clone, G: Clone,
impl<I, U> Clone for Flatten<I>where I: Clone + Iterator<Item: IntoIterator<IntoIter = U, Item = U::Item>>, U: Clone + Iterator,
impl<I: Clone + Iterator> Clone for Intersperse<I>where I::Item: Clone + Clone,
impl<I: Clone + Iterator> Clone for Peekable<I>where I::Item: Clone,
impl<I: Clone + Iterator, const N: usize> Clone for core::iter::adapters::array_chunks::ArrayChunks<I, N>where I::Item: Clone,
impl<I: Clone> Clone for FromIter<I>
impl<I: Clone> Clone for Cloned<I>
impl<I: Clone> Clone for Copied<I>
impl<I: Clone> Clone for Cycle<I>
impl<I: Clone> Clone for Enumerate<I>
impl<I: Clone> Clone for Fuse<I>
impl<I: Clone> Clone for Skip<I>
impl<I: Clone> Clone for StepBy<I>
impl<I: Clone> Clone for Take<I>
impl<I: Clone, F: Clone> Clone for FilterMap<I, F>
impl<I: Clone, F: Clone> Clone for Inspect<I, F>
impl<I: Clone, F: Clone> Clone for Map<I, F>
impl<I: Clone, P: Clone> Clone for Filter<I, P>
impl<I: Clone, P: Clone> Clone for MapWhile<I, P>
impl<I: Clone, P: Clone> Clone for SkipWhile<I, P>
impl<I: Clone, P: Clone> Clone for TakeWhile<I, P>
impl<I: Clone, St: Clone, F: Clone> Clone for Scan<I, St, F>
impl<I: Clone, U, F: Clone> Clone for FlatMap<I, U, F>where U: Clone + IntoIterator<IntoIter: Clone>,
impl<I: Clone, U: Clone> Clone for FlattenCompat<I, U>
impl<Idx: Clone> Clone for Range<Idx>
impl<Idx: Clone> Clone for RangeFrom<Idx>
impl<Idx: Clone> Clone for RangeInclusive<Idx>
impl<Idx: Clone> Clone for RangeTo<Idx>
impl<Idx: Clone> Clone for RangeToInclusive<Idx>
impl<P: Clone> Clone for Pin<P>
impl<Ret, T> Clone for fn (T₁, T₂, …, Tₙ) -> Ret
This trait is implemented on function pointers with any number of arguments.
impl<S: Sip> Clone for Hasher<S>
impl<T> Clone for Option<T>where T: Clone,
impl<T> Clone for Pending<T>
impl<T> Clone for Empty<T>
impl<T> Clone for Discriminant<T>
impl<T> Clone for core::result::Iter<'_, T>
impl<T> Clone for Chunks<'_, T>
impl<T> Clone for ChunksExact<'_, T>
impl<T> Clone for core::slice::iter::Iter<'_, T>
impl<T> Clone for RChunks<'_, T>
impl<T> Clone for Windows<'_, T>
impl<T, E> Clone for Result<T, E>where T: Clone, E: Clone,
impl<T, P> Clone for core::slice::iter::RSplit<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, P> Clone for core::slice::iter::Split<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, P> Clone for core::slice::iter::SplitInclusive<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, const LANES: usize> Clone for core::core_simd::masks::mask_impl::Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Clone for core::core_simd::masks::Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
impl<T, const N: usize> Clone for Simd<T, N>where LaneCount<N>: SupportedLaneCount, T: SimdElement,
impl<T, const N: usize> Clone for core::slice::iter::ArrayChunks<'_, T, N>
impl<T: Copy> Clone for Cell<T>
impl<T: Copy> Clone for Unaligned<T>
target_family="wasm"
only.impl<T: Copy> Clone for MaybeUninit<T>
impl<T: Clone + ?Sized> Clone for ManuallyDrop<T>
impl<T: Clone> Clone for Bound<T>
impl<T: Clone> Clone for Poll<T>
impl<T: Clone> Clone for (T₁, T₂, …, Tₙ)
This trait is implemented on arbitrary-length tuples.
impl<T: Clone> Clone for OnceCell<T>
impl<T: Clone> Clone for RefCell<T>
impl<T: Clone> Clone for Reverse<T>
impl<T: Clone> Clone for Ready<T>
impl<T: Clone> Clone for Rev<T>
impl<T: Clone> Clone for Once<T>
impl<T: Clone> Clone for Saturating<T>
impl<T: Clone> Clone for Wrapping<T>
impl<T: Clone> Clone for core::result::IntoIter<T>
impl<T: Clone, F: Clone> Clone for Successors<T, F>
impl<T: Clone, const N: usize> Clone for [T; N]
impl<T: Clone, const N: usize> Clone for core::array::iter::IntoIter<T, N>
impl<T: ?Sized> !Clone for &mut T
Shared references can be cloned, but mutable references cannot!
impl<T: ?Sized> Clone for *const T
impl<T: ?Sized> Clone for *mut T
impl<T: ?Sized> Clone for &T
Shared references can be cloned, but mutable references cannot!