Trait core::clone::Clone 1.0.0[−][src]
pub trait Clone: Sized { #[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
的资源,以避免不必要的分配。
Implementors
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on AArch64 only.
This is supported on PowerPC or PowerPC-64 only.
This is supported on PowerPC or PowerPC-64 only.
This is supported on PowerPC or PowerPC-64 only.
This is supported on PowerPC or PowerPC-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
This is supported on x86 or x86-64 only.
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;
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;
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;
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;
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;
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;
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;
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);
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;
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;
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;
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;
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;
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;
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];
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];
fn clone(&self) -> RepeatWith<F>ⓘNotable traits for RepeatWith<F>impl<A, F: FnMut() -> A> Iterator for RepeatWith<F> type Item = A;
[src]Notable traits for RepeatWith<F>
impl<A, F: FnMut() -> A> Iterator for RepeatWith<F> type Item = A;
fn clone(&self) -> Intersperse<I>ⓘNotable traits for Intersperse<I>impl<I> Iterator for Intersperse<I> where
I: Iterator,
I::Item: Clone, type Item = I::Item;
[src]Notable traits for Intersperse<I>
impl<I> Iterator for Intersperse<I> where
I: Iterator,
I::Item: Clone, type Item = I::Item;
fn clone(&self) -> DecodeUtf16<I>ⓘNotable traits for DecodeUtf16<I>impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> type Item = Result<char, DecodeUtf16Error>;
[src]Notable traits for DecodeUtf16<I>
impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> type Item = Result<char, DecodeUtf16Error>;
fn clone(&self) -> RangeInclusive<Idx>ⓘNotable traits for RangeInclusive<A>impl<A: Step> Iterator for RangeInclusive<A> type Item = A;
[src]Notable traits for RangeInclusive<A>
impl<A: Step> Iterator for RangeInclusive<A> type Item = A;
impl<T, P> Clone for core::slice::SplitInclusive<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
1.51.0[src]
impl<T, P> Clone for core::slice::SplitInclusive<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
1.51.0[src]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;
共享的引用可以被克隆,但是可变引用 不能!