Trait std::clone::Clone 1.0.0[−][src]
pub trait Clone {
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
。
// 当 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
(例如()
,(i32, bool)
),则为元组类型 - 闭包类型,如果它们没有从环境中捕获任何值,或者所有此类捕获的值本身都实现了
Clone
。 请注意,由共享引用捕获的变量始终实现Clone
(即使引用对象没有实现),而由变量引用捕获的变量从不实现Clone
。
Required methods
Provided methods
fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
从 source
执行复制分配。
a.clone_from(&b)
在功能上等同于 a = b.clone()
,但可以被覆盖以重用 a
的资源以避免不必要的分配。
Implementations on Foreign Types
Implementors
impl Clone for SocketCred
This is supported on (Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD) and Unix only.
共享的引用可以被克隆,但是可变引用 不能!
共享的引用可以被克隆,但是可变引用 不能!
1.27.0[src]
impl<'_, T, P> Clone for std::slice::RSplit<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
1.51.0[src]
impl<'_, T, P> Clone for std::slice::SplitInclusive<'_, T, P> where
P: Clone + FnMut(&T) -> bool,
1.5.0[src]
impl<'a, P> Clone for MatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
1.2.0[src]
impl<'a, P> Clone for Matches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
1.5.0[src]
impl<'a, P> Clone for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
1.2.0[src]
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 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,
1.51.0[src]
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 SplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<I> Clone for Intersperse<I> where
I: Clone + Iterator,
<I as Iterator>::Item: Clone,
<I as Iterator>::Item: Clone,
impl<I, G> Clone for IntersperseWith<I, G> where
I: Iterator + Clone,
G: Clone,
<I as Iterator>::Item: Clone,
1.29.0[src]