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 std::cmp::Ordering
impl Clone for TryReserveErrorKind
impl Clone for Infallible
impl Clone for VarError
impl Clone for std::fmt::Alignment
impl Clone for ErrorKind
impl Clone for SeekFrom
impl Clone for IpAddr
impl Clone for Ipv6MulticastScope
impl Clone for Shutdown
impl Clone for std::net::SocketAddr
impl Clone for FpCategory
impl Clone for IntErrorKind
impl Clone for BacktraceStyle
impl Clone for Which
impl Clone for SearchStep
impl Clone for std::sync::atomic::Ordering
impl Clone for RecvTimeoutError
impl Clone for TryRecvError
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 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 TimSortRun
impl Clone for AllocError
impl Clone for Global
impl Clone for Layout
impl Clone for LayoutError
impl Clone for System
impl Clone for TypeId
impl Clone for TryFromSliceError
impl Clone for std::ascii::EscapeDefault
impl Clone for Box<str, Global>
impl Clone for Box<CStr, Global>
impl Clone for Box<OsStr>
impl Clone for Box<Path>
impl Clone for CharTryFromError
impl Clone for DecodeUtf16Error
impl Clone for std::char::EscapeDebug
impl Clone for std::char::EscapeDefault
impl Clone for std::char::EscapeUnicode
impl Clone for ParseCharError
impl Clone for ToLowercase
impl Clone for ToUppercase
impl Clone for TryFromCharError
impl Clone for DefaultHasher
impl Clone for RandomState
impl Clone for TryReserveError
impl Clone for CString
impl Clone for FromBytesWithNulError
impl Clone for FromVecWithNulError
impl Clone for IntoStringError
impl Clone for NulError
impl Clone for OsString
impl Clone for Error
impl Clone for FileTimes
impl Clone for FileType
impl Clone for Metadata
impl Clone for OpenOptions
impl Clone for Permissions
impl Clone for SipHasher
impl Clone for std::io::Empty
impl Clone for Sink
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 stat
Available on Linux only.
impl Clone for std::os::unix::net::SocketAddr
Available on Unix only.
impl Clone for SocketCred
Available on (Android or Linux) and Unix only.
impl Clone for UCred
Available on Unix only.
impl Clone for InvalidHandleError
Available on Windows only.
impl Clone for NullHandleError
Available on Windows only.
impl Clone for PathBuf
impl Clone for StripPrefixError
impl Clone for ExitCode
impl Clone for ExitStatus
impl Clone for ExitStatusError
impl Clone for Output
impl Clone for std::ptr::Alignment
impl Clone for ParseBoolError
impl Clone for Utf8Error
impl Clone for FromUtf8Error
impl Clone for String
impl Clone for RecvError
impl Clone for WaitTimeoutResult
impl Clone for RawWakerVTable
impl Clone for Waker
impl Clone for AccessError
impl Clone for Thread
impl Clone for ThreadId
impl Clone for Duration
impl Clone for Instant
impl Clone for SystemTime
impl Clone for SystemTimeError
impl Clone for TryFromFloatSecsError
impl<'a> Clone for Component<'a>
impl<'a> Clone for Prefix<'a>
impl<'a> Clone for Source<'a>
impl<'a> Clone for Arguments<'a>
impl<'a> Clone for IoSlice<'a>
impl<'a> Clone for EncodeWide<'a>
impl<'a> Clone for Location<'a>
impl<'a> Clone for Ancestors<'a>
impl<'a> Clone for Components<'a>
impl<'a> Clone for std::path::Iter<'a>
impl<'a> Clone for PrefixComponent<'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 std::str::EscapeDebug<'a>
impl<'a> Clone for std::str::EscapeDefault<'a>
impl<'a> Clone for std::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: Clone + FnMut(char) -> bool,
impl<'a, P> Clone for MatchIndices<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for Matches<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RMatchIndices<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RMatches<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for std::str::RSplit<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RSplitN<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for RSplitTerminator<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for std::str::Split<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for std::str::SplitInclusive<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for SplitN<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for SplitTerminator<'a, P>where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: Clone,
impl<'a, T> Clone for RChunksExact<'a, T>
impl<'a, T, const N: usize> Clone for ArrayWindows<'a, T, N>where T: Clone + 'a,
impl<'a, const N: usize> Clone for CharArraySearcher<'a, N>
impl<'f> Clone for VaListImpl<'f>
impl<'fd> Clone for BorrowedFd<'fd>
impl<'handle> Clone for BorrowedHandle<'handle>
Available on Windows only.
impl<'socket> Clone for BorrowedSocket<'socket>
Available on Windows only.
impl<A> Clone for Repeat<A>where A: Clone,
impl<A> Clone for std::option::IntoIter<A>where A: Clone,
impl<A> Clone for std::option::Iter<'_, A>
impl<A, B> Clone for Chain<A, B>where A: Clone, B: Clone,
impl<A, B> Clone for Zip<A, B>where A: Clone, B: Clone,
impl<B> Clone for Cow<'_, B>where B: ToOwned + ?Sized,
impl<B, C> Clone for ControlFlow<B, C>where B: Clone, C: Clone,
impl<Dyn> Clone for DynMetadata<Dyn>where Dyn: ?Sized,
impl<F> Clone for FromFn<F>where F: Clone,
impl<F> Clone for OnceWith<F>where F: Clone,
impl<F> Clone for RepeatWith<F>where F: Clone,
impl<H> Clone for BuildHasherDefault<H>
impl<I> Clone for FromIter<I>where I: Clone,
impl<I> Clone for DecodeUtf16<I>where I: Clone + Iterator<Item = u16>,
impl<I> Clone for Cloned<I>where I: Clone,
impl<I> Clone for Copied<I>where I: Clone,
impl<I> Clone for Cycle<I>where I: Clone,
impl<I> Clone for Enumerate<I>where I: Clone,
impl<I> Clone for Fuse<I>where I: Clone,
impl<I> Clone for Intersperse<I>where I: Clone + Iterator, <I as Iterator>::Item: Clone,
impl<I> Clone for Peekable<I>where I: Clone + Iterator, <I as Iterator>::Item: Clone,
impl<I> Clone for Skip<I>where I: Clone,
impl<I> Clone for StepBy<I>where I: Clone,
impl<I> Clone for Take<I>where I: Clone,
impl<I, F> Clone for FilterMap<I, F>where I: Clone, F: Clone,
impl<I, F> Clone for Inspect<I, F>where I: Clone, F: Clone,
impl<I, F> Clone for Map<I, F>where I: Clone, F: Clone,
impl<I, G> Clone for IntersperseWith<I, G>where I: Iterator + Clone, <I as Iterator>::Item: Clone, G: Clone,
impl<I, P> Clone for Filter<I, P>where I: Clone, P: Clone,
impl<I, P> Clone for MapWhile<I, P>where I: Clone, P: Clone,
impl<I, P> Clone for SkipWhile<I, P>where I: Clone, P: Clone,
impl<I, P> Clone for TakeWhile<I, P>where I: Clone, P: Clone,
impl<I, St, F> Clone for Scan<I, St, F>where I: Clone, St: Clone, F: Clone,
impl<I, U> Clone for Flatten<I>where I: Clone + Iterator, <I as Iterator>::Item: IntoIterator<IntoIter = U, Item = <U as Iterator>::Item>, U: Clone + Iterator,
impl<I, U, F> Clone for FlatMap<I, U, F>where I: Clone, F: Clone, U: Clone + IntoIterator, <U as IntoIterator>::IntoIter: Clone,
impl<I, const N: usize> Clone for std::iter::ArrayChunks<I, N>where I: Clone + Iterator, <I as Iterator>::Item: Clone,
impl<Idx> Clone for std::ops::Range<Idx>where Idx: Clone,
impl<Idx> Clone for RangeFrom<Idx>where Idx: Clone,
impl<Idx> Clone for RangeInclusive<Idx>where Idx: Clone,
impl<Idx> Clone for RangeTo<Idx>where Idx: Clone,
impl<Idx> Clone for RangeToInclusive<Idx>where Idx: Clone,
impl<K> Clone for std::collections::hash_set::Iter<'_, K>
impl<K, V> Clone for std::collections::btree_map::Cursor<'_, K, V>
impl<K, V> Clone for std::collections::btree_map::Iter<'_, K, V>
impl<K, V> Clone for std::collections::btree_map::Keys<'_, K, V>
impl<K, V> Clone for std::collections::btree_map::Range<'_, K, V>
impl<K, V> Clone for std::collections::btree_map::Values<'_, K, V>
impl<K, V> Clone for std::collections::hash_map::Iter<'_, K, V>
impl<K, V> Clone for std::collections::hash_map::Keys<'_, K, V>
impl<K, V> Clone for std::collections::hash_map::Values<'_, K, V>
impl<K, V, A> Clone for BTreeMap<K, V, A>where K: Clone, V: Clone, A: Allocator + Clone,
impl<K, V, S> Clone for HashMap<K, V, S>where K: Clone, V: Clone, S: Clone,
impl<P> Clone for Pin<P>where P: Clone,
impl<Ret, T> Clone for fn (T₁, T₂, …, Tₙ) -> Ret
这个 trait 是在具有任意数量参数的函数指针上实现的。
impl<T> !Clone for &mut Twhere T: ?Sized,
共享的引用可以被克隆,但是可变引用 不能!
impl<T> Clone for Bound<T>where T: Clone,
impl<T> Clone for Option<T>where T: Clone,
impl<T> Clone for Poll<T>where T: Clone,
impl<T> Clone for *const Twhere T: ?Sized,
impl<T> Clone for *mut Twhere T: ?Sized,
impl<T> Clone for &Twhere T: ?Sized,
共享的引用可以被克隆,但是可变引用 不能!
impl<T> Clone for Cell<T>where T: Copy,
impl<T> Clone for OnceCell<T>where T: Clone,
impl<T> Clone for RefCell<T>where T: Clone,
impl<T> Clone for Reverse<T>where T: Clone,
impl<T> Clone for std::collections::binary_heap::IntoIter<T>where T: Clone,
impl<T> Clone for IntoIterSorted<T>where T: Clone,
impl<T> Clone for std::collections::binary_heap::Iter<'_, T>
impl<T> Clone for std::collections::btree_set::Iter<'_, T>
impl<T> Clone for std::collections::btree_set::Range<'_, T>
impl<T> Clone for std::collections::btree_set::SymmetricDifference<'_, T>
impl<T> Clone for std::collections::btree_set::Union<'_, T>
impl<T> Clone for std::collections::linked_list::Iter<'_, T>
impl<T> Clone for BinaryHeap<T>where T: Clone,
impl<T> Clone for std::collections::vec_deque::Iter<'_, T>
impl<T> Clone for Pending<T>
impl<T> Clone for Ready<T>where T: Clone,
impl<T> Clone for std::io::Cursor<T>where T: Clone,
impl<T> Clone for std::iter::Empty<T>
impl<T> Clone for Once<T>where T: Clone,
impl<T> Clone for Rev<T>where T: Clone,
impl<T> Clone for PhantomData<T>where T: ?Sized,
impl<T> Clone for Discriminant<T>
impl<T> Clone for ManuallyDrop<T>where T: Clone + ?Sized,
impl<T> Clone for Saturating<T>where T: Clone,
impl<T> Clone for Wrapping<T>where T: Clone,
impl<T> Clone for NonNull<T>where T: ?Sized,
impl<T> Clone for Rc<T>where T: ?Sized,
impl<T> Clone for std::rc::Weak<T>where T: ?Sized,
impl<T> Clone for std::result::IntoIter<T>where T: Clone,
impl<T> Clone for std::result::Iter<'_, T>
impl<T> Clone for Chunks<'_, T>
impl<T> Clone for ChunksExact<'_, T>
impl<T> Clone for std::slice::Iter<'_, T>
impl<T> Clone for RChunks<'_, T>
impl<T> Clone for Windows<'_, T>
impl<T> Clone for Sender<T>
impl<T> Clone for SyncSender<T>
impl<T> Clone for Arc<T>where T: ?Sized,
impl<T> Clone for std::sync::Weak<T>where T: ?Sized,
impl<T> Clone for MaybeUninit<T>where T: Copy,
impl<T, A> Clone for Box<[T], A>where T: Clone, A: Allocator + Clone,
impl<T, A> Clone for Box<T, A>where T: Clone, A: Allocator + Clone,
impl<T, A> Clone for std::collections::btree_set::Difference<'_, T, A>where A: Allocator + Clone,
impl<T, A> Clone for std::collections::btree_set::Intersection<'_, T, A>where A: Allocator + Clone,
impl<T, A> Clone for std::collections::linked_list::Cursor<'_, T, A>where A: Allocator,
impl<T, A> Clone for std::collections::linked_list::IntoIter<T, A>where T: Clone, A: Clone + Allocator,
impl<T, A> Clone for BTreeSet<T, A>where T: Clone, A: Allocator + Clone,
impl<T, A> Clone for LinkedList<T, A>where T: Clone, A: Allocator + Clone,
impl<T, A> Clone for VecDeque<T, A>where T: Clone, A: Allocator + Clone,
impl<T, A> Clone for std::collections::vec_deque::IntoIter<T, A>where T: Clone, A: Clone + Allocator,
impl<T, A> Clone for std::vec::IntoIter<T, A>where T: Clone, A: Allocator + Clone,
impl<T, A> Clone for Vec<T, A>where T: Clone, A: Allocator + Clone,
impl<T, E> Clone for Result<T, E>where T: Clone, E: Clone,
impl<T, F> Clone for Successors<T, F>where T: Clone, F: Clone,
impl<T, P> Clone for std::slice::RSplit<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, P> Clone for std::slice::Split<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, P> Clone for std::slice::SplitInclusive<'_, T, P>where P: Clone + FnMut(&T) -> bool,
impl<T, S> Clone for std::collections::hash_set::Difference<'_, T, S>
impl<T, S> Clone for HashSet<T, S>where T: Clone, S: Clone,
impl<T, S> Clone for std::collections::hash_set::Intersection<'_, T, S>
impl<T, S> Clone for std::collections::hash_set::SymmetricDifference<'_, T, S>
impl<T, S> Clone for std::collections::hash_set::Union<'_, T, S>
impl<T, const LANES: usize> Clone for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
impl<T, const N: usize> Clone for [T; N]where T: Clone,
impl<T, const N: usize> Clone for std::array::IntoIter<T, N>where T: Clone,
impl<T, const N: usize> Clone for Simd<T, N>where LaneCount<N>: SupportedLaneCount, T: SimdElement,
impl<T, const N: usize> Clone for std::slice::ArrayChunks<'_, T, N>
impl<T: Clone> Clone for TrySendError<T>
impl<T: Clone> Clone for (T₁, T₂, …, Tₙ)
这个 trait 在任意长度的元组上实现。