pub trait Clone: Sized {
// Required method
fn clone(&self) -> Self;
// Provided method
fn clone_from(&mut self, source: &Self) { ... }
}
Expand description
通用的 trait,用于显式复制对象。
与 Copy
的不同之处在于 Copy
是隐式的并且是廉价的按位复制,而 Clone
始终是显式的并且可能昂贵也可能不昂贵。
为了强制执行这些特性,Rust 不允许您重新实现 Copy
,但是您可以重新实现 Clone
并运行任意代码。
由于 Clone
比 Copy
更通用,因此您可以自动将 Copy
设为 Clone
。
Derivable
如果所有字段均为 Clone
,则此 trait 可以与 #[derive]
一起使用。Clone
的 derive
d 实现在每个字段上调用 clone
。
对于泛型结构体,#[derive]
通过在泛型参数上添加绑定的 Clone
有条件地实现 Clone
。
// 当 T 是 Clone 时,`derive` 为 Reading<T> 实现了 Clone。
#[derive(Clone)]
struct Reading<T> {
frequency: T,
}
Run如何实现 Clone
?
Copy
类型应该实现 Clone
的简单实现。更正式地:
如果 T: Copy
,x: T
和 y: &T
,则 let x = y.clone();
等效于 let x = *y;
。
手动执行时应注意保持不变。但是,不安全的代码一定不能依靠它来确保内存安全。
一个示例是持有函数指针的泛型结构体。在这种情况下,不能对 Clone
的实现进行派生操作,而可以将其实现为:
struct Generate<T>(fn() -> T);
impl<T> Copy for Generate<T> {}
impl<T> Clone for Generate<T> {
fn clone(&self) -> Self {
*self
}
}
Run其他实现者
除了下面列出的 实现者 外,以下类型还实现了 Clone
:
- 函数项类型 (即,为每个函数定义的不同类型)
- 函数指针类型 (例如
fn() -> i32
) - 闭包类型,如果它们没有从环境中捕获任何值,或者所有此类捕获的值本身都实现了
Clone
。 请注意,由共享引用捕获的变量始终实现Clone
(即使引用对象没有实现),而由变量引用捕获的变量从不实现Clone
。
Required Methods§
Provided Methods§
sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
从 source
执行复制分配。
a.clone_from(&b)
在功能上等同于 a = b.clone()
,但可以被覆盖以重用 a
的资源以避免不必要的分配。
Implementors§
impl Clone for AsciiChar
impl Clone for core::cmp::Ordering
impl Clone for Infallible
impl Clone for core::fmt::Alignment
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for SocketAddr
impl Clone for FpCategory
impl Clone for IntErrorKind
impl Clone for Which
impl Clone for SearchStep
impl Clone for core::sync::atomic::Ordering
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 AllocError
impl Clone for Layout
impl Clone for LayoutError
impl Clone for TypeId
impl Clone for float64x1_t
Available on AArch64 only.
impl Clone for float64x1x2_t
Available on AArch64 only.
impl Clone for float64x1x3_t
Available on AArch64 only.
impl Clone for float64x1x4_t
Available on AArch64 only.
impl Clone for float64x2_t
Available on AArch64 only.
impl Clone for float64x2x2_t
Available on AArch64 only.
impl Clone for float64x2x3_t
Available on AArch64 only.
impl Clone for float64x2x4_t
Available on AArch64 only.
impl Clone for int16x2_t
Available on ARM only.
impl Clone for uint16x2_t
Available on ARM only.
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 int8x4_t
Available on ARM only.
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 uint8x4_t
Available on ARM only.
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
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_bool_int
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_bool_long
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_bool_short
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_double
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_float
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_signed_char
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_signed_int
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_signed_long
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_signed_short
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_unsigned_char
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_unsigned_int
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_unsigned_long
Available on PowerPC or PowerPC-64 only.
impl Clone for vector_unsigned_short
Available on PowerPC or PowerPC-64 only.
impl Clone for v128
Available on
target_family="wasm"
only.impl Clone for CpuidResult
Available on x86 or x86-64 only.
impl Clone for __m128
Available on x86 or x86-64 only.
impl Clone for __m128bh
Available on x86 or x86-64 only.
impl Clone for __m128d
Available on x86 or x86-64 only.
impl Clone for __m128i
Available on x86 or x86-64 only.
impl Clone for __m256
Available on x86 or x86-64 only.
impl Clone for __m256bh
Available on x86 or x86-64 only.
impl Clone for __m256d
Available on x86 or x86-64 only.
impl Clone for __m256i
Available on x86 or x86-64 only.
impl Clone for __m512
Available on x86 or x86-64 only.
impl Clone for __m512bh
Available on x86 or x86-64 only.
impl Clone for __m512d
Available on x86 or x86-64 only.
impl Clone for __m512i
Available on x86 or x86-64 only.
impl Clone for TryFromSliceError
impl Clone for core::ascii::EscapeDefault
impl Clone for CharTryFromError
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 ParseCharError
impl Clone for ToLowercase
impl Clone for ToUppercase
impl Clone for TryFromCharError
impl Clone for FromBytesUntilNulError
impl Clone for FromBytesWithNulError
impl Clone for Error
impl Clone for SipHasher
impl Clone for PhantomPinned
impl Clone for Assume
impl Clone for AddrParseError
impl Clone for Ipv4Addr
impl Clone for Ipv6Addr
impl Clone for SocketAddrV4
impl Clone for SocketAddrV6
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 ParseIntError
impl Clone for TryFromIntError
impl Clone for RangeFull
impl Clone for core::ptr::Alignment
impl Clone for TimSortRun
impl Clone for ParseBoolError
impl Clone for Utf8Error
impl Clone for RawWakerVTable
impl Clone for Waker
impl Clone for Duration
impl Clone for TryFromFloatSecsError
impl<'a> Clone for Source<'a>
impl<'a> Clone for Arguments<'a>
impl<'a> Clone for Location<'a>
impl<'a> Clone for EscapeAscii<'a>
impl<'a> Clone for CharSearcher<'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::EscapeDebug<'a>
impl<'a> Clone for core::str::EscapeDefault<'a>
impl<'a> Clone for core::str::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, '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, 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 Matches<'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::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::Split<'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 SplitTerminator<'a, P>where P: Pattern<'a, Searcher: Clone>,
impl<'a, P: Pattern<'a, Searcher: Clone>> Clone for core::str::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, 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<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::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<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
这个 trait 是在具有任意数量参数的函数指针上实现的。
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<'_, 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::RSplit<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, P> Clone for core::slice::Split<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, P> Clone for core::slice::SplitInclusive<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, const LANES: usize> Clone for 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::ArrayChunks<'_, T, N>
impl<T: Copy> Clone for Cell<T>
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ₙ)
这个 trait 在任意长度的元组上实现。
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 Once<T>
impl<T: Clone> Clone for Rev<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::IntoIter<T, N>
impl<T: ?Sized> !Clone for &mut T
共享的引用可以被克隆,但是可变引用 不能!
impl<T: ?Sized> Clone for *const T
impl<T: ?Sized> Clone for *mut T
impl<T: ?Sized> Clone for &T
共享的引用可以被克隆,但是可变引用 不能!