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
A common trait for the ability to explicitly duplicate an object.
Differs from Copy
in that Copy
is implicit and extremely inexpensive, 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, }Run
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 } }Run
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)
[src]
fn clone_from(&mut self, source: &Self)
[src]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) -> 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]impl<'a, T, P> Iterator for SplitInclusive<'a, T, P> where
P: FnMut(&T) -> bool, type Item = &'a [T];
pub fn clone(&self) -> EscapeAscii<'a>ⓘNotable traits for EscapeAscii<'a>impl<'a> Iterator for EscapeAscii<'a> type Item = u8;
[src]impl<'a> Iterator for EscapeAscii<'a> type Item = u8;
Implementors
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = u8;
[src]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]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]impl Iterator for EscapeDebug type Item = char;
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = char;
[src]impl Iterator for EscapeDefault type Item = char;
pub fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicodeimpl Iterator for EscapeUnicode type Item = char;
[src]impl Iterator for EscapeUnicode type Item = char;
pub fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercaseimpl Iterator for ToLowercase type Item = char;
[src]impl Iterator for ToLowercase type Item = char;
pub fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercaseimpl Iterator for ToUppercase type Item = char;
[src]impl Iterator for ToUppercase type Item = char;
Shared references can be cloned, but mutable references cannot!
Shared references can be cloned, but mutable references cannot!
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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]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]impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
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]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]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]impl<T, F> Iterator for Successors<T, F> where
F: FnMut(&T) -> Option<T>, type Item = T;