Trait sp_std::clone::Clone 1.0.0[−][src]
pub trait Clone { fn clone(&self) -> Self; fn clone_from(&mut self, source: &Self) { ... } }
Expand description
A common trait for the ability to explicitly duplicate an object.
Differs from Copy
in that Copy
is implicit and an inexpensive bit-wise copy, while
Clone
is always explicit and may or may not be expensive. In order to enforce
these characteristics, Rust does not allow you to reimplement Copy
, but you
may reimplement Clone
and run arbitrary code.
Since Clone
is more general than Copy
, you can automatically make anything
Copy
be Clone
as well.
Derivable
This trait can be used with #[derive]
if all fields are Clone
. The derive
d
implementation of Clone
calls clone
on each field.
For a generic struct, #[derive]
implements Clone
conditionally by adding bound Clone
on
generic parameters.
// `derive` implements Clone for Reading<T> when T is Clone. #[derive(Clone)] struct Reading<T> { frequency: T, }
How can I implement Clone
?
Types that are Copy
should have a trivial implementation of Clone
. More formally:
if T: Copy
, x: T
, and y: &T
, then let x = y.clone();
is equivalent to let x = *y;
.
Manual implementations should be careful to uphold this invariant; however, unsafe code
must not rely on it to ensure memory safety.
An example is a generic struct holding a function pointer. In this case, the
implementation of Clone
cannot be derive
d, but can be implemented as:
struct Generate<T>(fn() -> T); impl<T> Copy for Generate<T> {} impl<T> Clone for Generate<T> { fn clone(&self) -> Self { *self } }
Additional implementors
In addition to the implementors listed below,
the following types also implement Clone
:
- Function item types (i.e., the distinct types defined for each function)
- Function pointer types (e.g.,
fn() -> i32
) - Array types, for all sizes, if the item type also implements
Clone
(e.g.,[i32; 123456]
) - Tuple types, if each component also implements
Clone
(e.g.,()
,(i32, bool)
) - Closure types, if they capture no value from the environment
or if all such captured values implement
Clone
themselves. Note that variables captured by shared reference always implementClone
(even if the referent doesn’t), while variables captured by mutable reference never implementClone
.
Required methods
Provided methods
fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
.
a.clone_from(&b)
is equivalent to a = b.clone()
in functionality,
but can be overridden to reuse the resources of a
to avoid unnecessary
allocations.
Implementations on Foreign Types
pub fn clone(&self) -> Intersection<'_, T, S>ⓘNotable traits for Intersection<'a, T, S>
impl<'a, T, S> Iterator for Intersection<'a, T, S> where
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
pub fn clone(&self) -> Components<'a>ⓘNotable traits for Components<'a>
impl<'a> Iterator for Components<'a> type Item = Component<'a>;
pub fn clone(&self) -> SymmetricDifference<'_, T, S>ⓘNotable traits for SymmetricDifference<'a, T, S>
impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S> where
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
pub fn clone(&self) -> Difference<'_, T, S>ⓘNotable traits for Difference<'a, T, S>
impl<'a, T, S> Iterator for Difference<'a, T, S> where
T: Eq + Hash,
S: BuildHasher, type Item = &'a T;
pub fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicode
impl Iterator for EscapeUnicode type Item = char;
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefault
impl Iterator for EscapeDefault type Item = char;
pub fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercase
impl Iterator for ToUppercase type Item = char;
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>;
pub fn clone(&self) -> EscapeDebugⓘNotable traits for EscapeDebug
impl Iterator for EscapeDebug type Item = char;
Shared references can be cloned, but mutable references cannot!
pub fn clone(&self) -> EscapeAscii<'a>ⓘNotable traits for EscapeAscii<'a>
impl<'a> Iterator for EscapeAscii<'a> type Item = u8;
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefault
impl Iterator for EscapeDefault type Item = u8;
Shared references can be cloned, but mutable references cannot!
pub fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercase
impl Iterator for ToLowercase type Item = char;
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];
pub fn clone(&self) -> IntoIterSorted<T>ⓘNotable traits for IntoIterSorted<T>
impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
Implementors
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 sp_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 sp_std::str::Split<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Clone,
impl<'a, P> Clone for sp_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<'a, T, P> Clone for sp_std::slice::RSplit<'a, T, P> where
T: 'a + Clone,
P: Clone + FnMut(&T) -> bool,
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,
impl<I, U> Clone for Flatten<I> where
I: Clone + Iterator,
U: Clone + Iterator,
<I as Iterator>::Item: IntoIterator,
<<I as Iterator>::Item as IntoIterator>::IntoIter == U,
<<I as Iterator>::Item as IntoIterator>::Item == <U 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,