Trait std::clone::Clone 1.0.0[−][src]
pub trait Clone { #[must_use = "cloning is often expensive and is not expected to have side effects"] fn clone(&self) -> Self; 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
。
// `derive` T 为 Clone 时,为 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
(例如[i32; 123456]
),则所有大小的数组类型 - 如果每个组件还实现
Clone
(例如()
,(i32, bool)
),则为元组类型 - 闭包类型,如果它们没有从环境中捕获任何值,或者所有此类捕获的值本身都实现了
Clone
。 请注意,由共享引用捕获的变量始终实现Clone
(即使引用对象没有实现),而由变量引用捕获的变量从不实现Clone
。
Required methods
Provided methods
fn clone_from(&mut self, source: &Self)
[src]
fn clone_from(&mut self, source: &Self)
[src]从 source
执行复制分配。
a.clone_from(&b)
在功能上等效于 a = b.clone()
,但是可以重写以重用 a
的资源,以避免不必要的分配。
Implementations on Foreign Types
pub fn clone(&self) -> EscapeAscii<'a>ⓘNotable traits for EscapeAscii<'a>impl<'a> Iterator for EscapeAscii<'a> type Item = u8;
[src]Notable traits for EscapeAscii<'a>
impl<'a> Iterator for EscapeAscii<'a> type Item = u8;
pub fn clone(&self) -> SplitInclusive<'_, T, P>ⓘNotable traits for SplitInclusive<'a, T, P>impl<'a, T, P> Iterator for SplitInclusive<'a, T, P> where
P: FnMut(&T) -> bool, type Item = &'a [T];
[src]Notable traits for SplitInclusive<'a, T, P>
impl<'a, T, P> Iterator for SplitInclusive<'a, T, P> where
P: FnMut(&T) -> bool, type Item = &'a [T];
Implementors
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = u8;
[src]Notable traits for EscapeDefault
impl Iterator for EscapeDefault type Item = u8;
pub fn clone(&self) -> Box<str, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
pub fn clone(&self) -> EscapeDebugⓘNotable traits for EscapeDebugimpl Iterator for EscapeDebug type Item = char;
[src]Notable traits for EscapeDebug
impl Iterator for EscapeDebug type Item = char;
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = char;
[src]Notable traits for EscapeDefault
impl Iterator for EscapeDefault type Item = char;
pub fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicodeimpl Iterator for EscapeUnicode type Item = char;
[src]Notable traits for EscapeUnicode
impl Iterator for EscapeUnicode type Item = char;
pub fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercaseimpl Iterator for ToLowercase type Item = char;
[src]Notable traits for ToLowercase
impl Iterator for ToLowercase type Item = char;
pub fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercaseimpl Iterator for ToUppercase type Item = char;
[src]Notable traits for ToUppercase
impl Iterator for ToUppercase type Item = char;
This is supported on Unix only.
This is supported on (Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD) and Unix only.
共享的引用可以被克隆,但是可变引用 不能!
共享的引用可以被克隆,但是可变引用 不能!
pub fn clone(&self) -> Difference<'_, T>ⓘNotable traits for Difference<'a, T>impl<'a, T> Iterator for Difference<'a, T> where
T: Ord, type Item = &'a T;
[src]Notable traits for Difference<'a, T>
impl<'a, T> Iterator for Difference<'a, T> where
T: Ord, type Item = &'a T;
pub fn clone(&self) -> Intersection<'_, T>ⓘNotable traits for Intersection<'a, T>impl<'a, T> Iterator for Intersection<'a, T> where
T: Ord, type Item = &'a T;
[src]Notable traits for Intersection<'a, T>
impl<'a, T> Iterator for Intersection<'a, T> where
T: Ord, type Item = &'a T;
pub fn clone(&self) -> SymmetricDifference<'_, T>ⓘNotable traits for SymmetricDifference<'a, T>impl<'a, T> Iterator for SymmetricDifference<'a, T> where
T: Ord, type Item = &'a T;
[src]Notable traits for SymmetricDifference<'a, T>
impl<'a, T> Iterator for SymmetricDifference<'a, T> where
T: Ord, type Item = &'a T;
pub fn clone(&self) -> ChunksExact<'_, T>ⓘNotable traits for ChunksExact<'a, T>impl<'a, T> Iterator for ChunksExact<'a, T> type Item = &'a [T];
[src]Notable traits for ChunksExact<'a, T>
impl<'a, T> Iterator for ChunksExact<'a, T> type Item = &'a [T];
pub fn clone(&self) -> ArrayChunks<'_, T, N>ⓘNotable traits for ArrayChunks<'a, T, N>impl<'a, T, const N: usize> Iterator for ArrayChunks<'a, T, N> type Item = &'a [T; N];
[src]Notable traits for ArrayChunks<'a, T, N>
impl<'a, T, const N: usize> Iterator for ArrayChunks<'a, T, N> type Item = &'a [T; N];
fn clone(&self) -> EncodeWide<'a>ⓘNotable traits for EncodeWide<'a>impl<'a> Iterator for EncodeWide<'a> type Item = u16;
[src]Notable traits for EncodeWide<'a>
impl<'a> Iterator for EncodeWide<'a> type Item = u16;
fn clone(&self) -> Components<'a>ⓘNotable traits for Components<'a>impl<'a> Iterator for Components<'a> type Item = Component<'a>;
[src]Notable traits for Components<'a>
impl<'a> Iterator for Components<'a> type Item = Component<'a>;
pub fn clone(&self) -> CharIndices<'a>ⓘNotable traits for CharIndices<'a>impl<'a> Iterator for CharIndices<'a> type Item = (usize, char);
[src]Notable traits for CharIndices<'a>
impl<'a> Iterator for CharIndices<'a> type Item = (usize, char);
pub fn clone(&self) -> EncodeUtf16<'a>ⓘNotable traits for EncodeUtf16<'a>impl<'a> Iterator for EncodeUtf16<'a> type Item = u16;
[src]Notable traits for EncodeUtf16<'a>
impl<'a> Iterator for EncodeUtf16<'a> type Item = u16;
pub fn clone(&self) -> EscapeDebug<'a>ⓘNotable traits for EscapeDebug<'a>impl<'a> Iterator for EscapeDebug<'a> type Item = char;
[src]Notable traits for EscapeDebug<'a>
impl<'a> Iterator for EscapeDebug<'a> type Item = char;
pub fn clone(&self) -> EscapeDefault<'a>ⓘNotable traits for EscapeDefault<'a>impl<'a> Iterator for EscapeDefault<'a> type Item = char;
[src]Notable traits for EscapeDefault<'a>
impl<'a> Iterator for EscapeDefault<'a> type Item = char;
pub fn clone(&self) -> EscapeUnicode<'a>ⓘNotable traits for EscapeUnicode<'a>impl<'a> Iterator for EscapeUnicode<'a> type Item = char;
[src]Notable traits for EscapeUnicode<'a>
impl<'a> Iterator for EscapeUnicode<'a> type Item = char;
pub fn clone(&self) -> SplitAsciiWhitespace<'a>ⓘNotable traits for SplitAsciiWhitespace<'a>impl<'a> Iterator for SplitAsciiWhitespace<'a> type Item = &'a str;
[src]Notable traits for SplitAsciiWhitespace<'a>
impl<'a> Iterator for SplitAsciiWhitespace<'a> type Item = &'a str;
pub fn clone(&self) -> SplitWhitespace<'a>ⓘNotable traits for SplitWhitespace<'a>impl<'a> Iterator for SplitWhitespace<'a> type Item = &'a str;
[src]Notable traits for SplitWhitespace<'a>
impl<'a> Iterator for SplitWhitespace<'a> type Item = &'a str;
pub fn clone(&self) -> MatchIndices<'a, P>ⓘNotable traits for MatchIndices<'a, P>impl<'a, P> Iterator for MatchIndices<'a, P> where
P: Pattern<'a>, type Item = (usize, &'a str);
[src]Notable traits for MatchIndices<'a, P>
impl<'a, P> Iterator for MatchIndices<'a, P> where
P: Pattern<'a>, type Item = (usize, &'a str);
pub fn clone(&self) -> RMatchIndices<'a, P>ⓘNotable traits for RMatchIndices<'a, P>impl<'a, P> Iterator for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>, type Item = (usize, &'a str);
[src]Notable traits for RMatchIndices<'a, P>
impl<'a, P> Iterator for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>, type Item = (usize, &'a str);
pub fn clone(&self) -> RSplitTerminator<'a, P>ⓘNotable traits for RSplitTerminator<'a, P>impl<'a, P> Iterator for RSplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>, type Item = &'a str;
[src]Notable traits for RSplitTerminator<'a, P>
impl<'a, P> Iterator for RSplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>, type Item = &'a str;
pub fn clone(&self) -> SplitInclusive<'a, P>ⓘNotable traits for SplitInclusive<'a, P>impl<'a, P> Iterator for SplitInclusive<'a, P> where
P: Pattern<'a>, type Item = &'a str;
[src]Notable traits for SplitInclusive<'a, P>
impl<'a, P> Iterator for SplitInclusive<'a, P> where
P: Pattern<'a>, type Item = &'a str;
pub fn clone(&self) -> SplitTerminator<'a, P>ⓘNotable traits for SplitTerminator<'a, P>impl<'a, P> Iterator for SplitTerminator<'a, P> where
P: Pattern<'a>, type Item = &'a str;
[src]Notable traits for SplitTerminator<'a, P>
impl<'a, P> Iterator for SplitTerminator<'a, P> where
P: Pattern<'a>, type Item = &'a str;
pub fn clone(&self) -> RChunksExact<'a, T>ⓘNotable traits for RChunksExact<'a, T>impl<'a, T> Iterator for RChunksExact<'a, T> type Item = &'a [T];
[src]Notable traits for RChunksExact<'a, T>
impl<'a, T> Iterator for RChunksExact<'a, T> type Item = &'a [T];
pub fn clone(&self) -> ArrayWindows<'a, T, N>ⓘNotable traits for ArrayWindows<'a, T, N>impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> type Item = &'a [T; N];
[src]Notable traits for ArrayWindows<'a, T, N>
impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> type Item = &'a [T; N];
pub fn clone(&self) -> RepeatWith<F>ⓘNotable traits for RepeatWith<F>impl<A, F> Iterator for RepeatWith<F> where
F: FnMut() -> A, type Item = A;
[src]Notable traits for RepeatWith<F>
impl<A, F> Iterator for RepeatWith<F> where
F: FnMut() -> A, type Item = A;
pub fn clone(&self) -> DecodeUtf16<I>ⓘNotable traits for DecodeUtf16<I>impl<I> Iterator for DecodeUtf16<I> where
I: Iterator<Item = u16>, type Item = Result<char, DecodeUtf16Error>;
[src]Notable traits for DecodeUtf16<I>
impl<I> Iterator for DecodeUtf16<I> where
I: Iterator<Item = u16>, type Item = Result<char, DecodeUtf16Error>;
pub fn clone(&self) -> Intersperse<I>ⓘNotable traits for Intersperse<I>impl<I> Iterator for Intersperse<I> where
I: Iterator,
<I as Iterator>::Item: Clone, type Item = <I as Iterator>::Item;
[src]Notable traits for Intersperse<I>
impl<I> Iterator for Intersperse<I> where
I: Iterator,
<I as Iterator>::Item: Clone, type Item = <I as Iterator>::Item;
pub fn clone(&self) -> IntersperseWith<I, G>ⓘNotable traits for IntersperseWith<I, G>impl<I, G> Iterator for IntersperseWith<I, G> where
I: Iterator,
G: FnMut() -> <I as Iterator>::Item, type Item = <I as Iterator>::Item;
[src]Notable traits for IntersperseWith<I, G>
impl<I, G> Iterator for IntersperseWith<I, G> where
I: Iterator,
G: FnMut() -> <I as Iterator>::Item, type Item = <I as Iterator>::Item;
impl<I, U, F> Clone for FlatMap<I, U, F> where
F: Clone,
I: Clone,
U: Clone + IntoIterator,
<U as IntoIterator>::IntoIter: Clone,
[src]
impl<I, U, F> Clone for FlatMap<I, U, F> where
F: Clone,
I: Clone,
U: Clone + IntoIterator,
<U as IntoIterator>::IntoIter: Clone,
[src]pub fn clone(&self) -> RangeInclusive<Idx>ⓘNotable traits for RangeInclusive<A>impl<A> Iterator for RangeInclusive<A> where
A: Step, type Item = A;
[src]Notable traits for RangeInclusive<A>
impl<A> Iterator for RangeInclusive<A> where
A: Step, type Item = A;
pub fn clone(&self) -> IntoIterSorted<T>ⓘNotable traits for IntoIterSorted<T>impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
[src]Notable traits for IntoIterSorted<T>
impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
克隆发送者以发送到其他线程。
请注意,请注意发送方的生命周期,因为所有发送方 (包括原始发送方) 都需要丢弃,以便 Receiver::recv
停止阻塞。
pub fn clone(&self) -> Box<[T], A>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
pub fn clone(&self) -> Box<T, A>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
pub fn clone(&self) -> Box<T, A>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
pub fn clone(&self) -> Successors<T, F>ⓘNotable traits for Successors<T, F>impl<T, F> Iterator for Successors<T, F> where
F: FnMut(&T) -> Option<T>, type Item = T;
[src]Notable traits for Successors<T, F>
impl<T, F> Iterator for Successors<T, F> where
F: FnMut(&T) -> Option<T>, type Item = T;