Trait sexually_transmitted_disease::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, }
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)
[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]Shared references can be cloned, but mutable references cannot!
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;
[src]Implementors
pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefault
impl Iterator for EscapeDefault type Item = u8;
[src]pub fn clone(&self) -> Box<str, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]pub fn clone(&self) -> Box<CStr, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]pub fn clone(&self) -> Box<OsStr, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]pub fn clone(&self) -> Box<Path, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]pub fn clone(&self) -> EscapeDebugⓘNotable traits for EscapeDebug
impl Iterator for EscapeDebug type Item = char;
[src]pub fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefault
impl Iterator for EscapeDefault type Item = char;
[src]pub fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicode
impl Iterator for EscapeUnicode type Item = char;
[src]pub fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercase
impl Iterator for ToLowercase type Item = char;
[src]pub fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercase
impl Iterator for ToUppercase type Item = char;
[src]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<'_, T> Clone for sexually_transmitted_disease::collections::btree_set::Intersection<'_, T>
[src]
impl<'_, T> Clone for sexually_transmitted_disease::collections::btree_set::Intersection<'_, T>
[src]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<'_, T> Clone for sexually_transmitted_disease::collections::btree_set::SymmetricDifference<'_, T>
[src]
impl<'_, T> Clone for sexually_transmitted_disease::collections::btree_set::SymmetricDifference<'_, T>
[src]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]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<'_, T, S> Clone for sexually_transmitted_disease::collections::hash_set::Difference<'_, T, S>
[src]
impl<'_, T, S> Clone for sexually_transmitted_disease::collections::hash_set::Difference<'_, T, S>
[src]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;
[src]impl<'_, T, S> Clone for sexually_transmitted_disease::collections::hash_set::Intersection<'_, T, S>
[src]
impl<'_, T, S> Clone for sexually_transmitted_disease::collections::hash_set::Intersection<'_, T, S>
[src]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;
[src]impl<'_, T, S> Clone for sexually_transmitted_disease::collections::hash_set::SymmetricDifference<'_, T, S>
[src]
impl<'_, T, S> Clone for sexually_transmitted_disease::collections::hash_set::SymmetricDifference<'_, T, S>
[src]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;
[src]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]pub fn clone(&self) -> EncodeWide<'a>ⓘNotable traits for EncodeWide<'a>
impl<'a> Iterator for EncodeWide<'a> type Item = u16;
[src]pub fn clone(&self) -> Components<'a>ⓘNotable traits for Components<'a>
impl<'a> Iterator for Components<'a> type Item = Component<'a>;
[src]pub fn clone(&self) -> CharIndices<'a>ⓘNotable traits for CharIndices<'a>
impl<'a> Iterator for CharIndices<'a> type Item = (usize, char);
[src]pub fn clone(&self) -> EncodeUtf16<'a>ⓘNotable traits for EncodeUtf16<'a>
impl<'a> Iterator for EncodeUtf16<'a> type Item = u16;
[src]pub fn clone(&self) -> EscapeDebug<'a>ⓘNotable traits for EscapeDebug<'a>
impl<'a> Iterator for EscapeDebug<'a> type Item = char;
[src]pub fn clone(&self) -> EscapeDefault<'a>ⓘNotable traits for EscapeDefault<'a>
impl<'a> Iterator for EscapeDefault<'a> type Item = char;
[src]pub fn clone(&self) -> EscapeUnicode<'a>ⓘNotable traits for EscapeUnicode<'a>
impl<'a> Iterator for EscapeUnicode<'a> type Item = char;
[src]pub fn clone(&self) -> SplitAsciiWhitespace<'a>ⓘNotable traits for SplitAsciiWhitespace<'a>
impl<'a> Iterator for SplitAsciiWhitespace<'a> type Item = &'a str;
[src]pub fn clone(&self) -> SplitWhitespace<'a>ⓘNotable traits for SplitWhitespace<'a>
impl<'a> Iterator for SplitWhitespace<'a> type Item = &'a str;
[src]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]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]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]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]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]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]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]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]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]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]pub fn clone(&self) -> IntersperseWith<I, G>ⓘNotable traits for IntersperseWith<I, G>
impl<I, G> Iterator for IntersperseWith<I, G> where
G: FnMut() -> <I as Iterator>::Item,
I: Iterator, type Item = <I as Iterator>::Item;
[src]impl<I, U, F> Clone for FlatMap<I, U, F> where
F: Clone,
U: Clone + IntoIterator,
I: Clone,
<U as IntoIterator>::IntoIter: Clone,
[src]
impl<I, U, F> Clone for FlatMap<I, U, F> where
F: Clone,
U: Clone + IntoIterator,
I: Clone,
<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]pub fn clone(&self) -> IntoIterSorted<T>ⓘNotable traits for IntoIterSorted<T>
impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
[src]pub fn clone(&self) -> Box<[T], A>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]pub fn clone(&self) -> Box<T, A>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]
pub fn clone(&self) -> Box<T, A>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, 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;
[src]Returns a new box with a clone()
of this box’s contents.
Examples
let x = Box::new(5); let y = x.clone(); // The value is the same assert_eq!(x, y); // But they are unique objects assert_ne!(&*x as *const i32, &*y as *const i32);
Copies source
’s contents into self
without creating a new allocation.
Examples
let x = Box::new(5); let mut y = Box::new(10); let yp: *const i32 = &*y; y.clone_from(&x); // The value is the same assert_eq!(x, y); // And no allocation occurred assert_eq!(yp, &*y);
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]