Trait signature_core::lib::Clone
1.0.0 · source · [−]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
) - 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
1.55.0 · sourceimpl<B, C> Clone for ControlFlow<B, C> where
B: Clone,
C: Clone,
impl<B, C> Clone for ControlFlow<B, C> where
B: Clone,
C: Clone,
fn clone(&self) -> ControlFlow<B, C>
sourceimpl Clone for AllocError
impl Clone for AllocError
fn clone(&self) -> AllocError
sourceimpl<'f> Clone for VaListImpl<'f>
impl<'f> Clone for VaListImpl<'f>
fn clone(&self) -> VaListImpl<'f>
sourceimpl<Y, R> Clone for GeneratorState<Y, R> where
Y: Clone,
R: Clone,
impl<Y, R> Clone for GeneratorState<Y, R> where
Y: Clone,
R: Clone,
fn clone(&self) -> GeneratorState<Y, R>
1.36.0 · sourceimpl Clone for RawWakerVTable
impl Clone for RawWakerVTable
fn clone(&self) -> RawWakerVTable
1.59.0 · sourceimpl Clone for TryFromCharError
impl Clone for TryFromCharError
fn clone(&self) -> TryFromCharError
sourceimpl Clone for ToLowercase
impl Clone for ToLowercase
fn clone(&self) -> ToLowercaseⓘNotable traits for ToLowercaseimpl Iterator for ToLowercase type Item = char;
1.7.0 · sourceimpl<H> Clone for BuildHasherDefault<H>
impl<H> Clone for BuildHasherDefault<H>
fn clone(&self) -> BuildHasherDefault<H>
1.9.0 · sourceimpl<I> Clone for DecodeUtf16<I> where
I: Clone + Iterator<Item = u16>,
impl<I> Clone for DecodeUtf16<I> where
I: Clone + Iterator<Item = u16>,
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>;
I: Iterator<Item = u16>, type Item = Result<char, DecodeUtf16Error>;
1.34.0 · sourceimpl Clone for CharTryFromError
impl Clone for CharTryFromError
fn clone(&self) -> CharTryFromError
sourceimpl Clone for ToUppercase
impl Clone for ToUppercase
fn clone(&self) -> ToUppercaseⓘNotable traits for ToUppercaseimpl Iterator for ToUppercase type Item = char;
sourceimpl Clone for FromBytesUntilNulError
impl Clone for FromBytesUntilNulError
fn clone(&self) -> FromBytesUntilNulError
1.9.0 · sourceimpl Clone for DecodeUtf16Error
impl Clone for DecodeUtf16Error
fn clone(&self) -> DecodeUtf16Error
sourceimpl Clone for EscapeDefault
impl Clone for EscapeDefault
fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = char;
1.34.0 · sourceimpl Clone for TryFromSliceError
impl Clone for TryFromSliceError
fn clone(&self) -> TryFromSliceError
sourceimpl Clone for FromFloatSecsError
impl Clone for FromFloatSecsError
fn clone(&self) -> FromFloatSecsError
const: unstable · sourceimpl<'_, T> Clone for &'_ T where
T: ?Sized,
impl<'_, T> Clone for &'_ T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!
sourceimpl Clone for EscapeUnicode
impl Clone for EscapeUnicode
fn clone(&self) -> EscapeUnicodeⓘNotable traits for EscapeUnicodeimpl Iterator for EscapeUnicode type Item = char;
1.20.0 · sourceimpl Clone for ParseCharError
impl Clone for ParseCharError
fn clone(&self) -> ParseCharError
1.20.0 · sourceimpl Clone for EscapeDebug
impl Clone for EscapeDebug
fn clone(&self) -> EscapeDebugⓘNotable traits for EscapeDebugimpl Iterator for EscapeDebug type Item = char;
1.50.0 · sourceimpl Clone for LayoutError
impl Clone for LayoutError
fn clone(&self) -> LayoutError
sourceimpl Clone for EscapeDefault
impl Clone for EscapeDefault
fn clone(&self) -> EscapeDefaultⓘNotable traits for EscapeDefaultimpl Iterator for EscapeDefault type Item = u8;
sourceimpl<T, const LANES: usize> Clone for Simd<T, LANES> where
T: SimdElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Clone for Simd<T, LANES> where
T: SimdElement,
LaneCount<LANES>: SupportedLaneCount,
impl<'_, T> !Clone for &'_ mut T where
T: ?Sized,
Shared references can be cloned, but mutable references cannot!
1.27.0 · sourceimpl Clone for CpuidResult
impl Clone for CpuidResult
fn clone(&self) -> CpuidResult
sourceimpl<Dyn> Clone for DynMetadata<Dyn> where
Dyn: ?Sized,
impl<Dyn> Clone for DynMetadata<Dyn> where
Dyn: ?Sized,
fn clone(&self) -> DynMetadata<Dyn>
sourceimpl<T, const LANES: usize> Clone for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Clone for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
1.26.0 · sourceimpl<Idx> Clone for RangeToInclusive<Idx> where
Idx: Clone,
impl<Idx> Clone for RangeToInclusive<Idx> where
Idx: Clone,
fn clone(&self) -> RangeToInclusive<Idx>
sourceimpl Clone for FromBytesWithNulError
impl Clone for FromBytesWithNulError
fn clone(&self) -> FromBytesWithNulError
1.26.0 · sourceimpl<Idx> Clone for RangeInclusive<Idx> where
Idx: Clone,
impl<Idx> Clone for RangeInclusive<Idx> where
Idx: Clone,
fn clone(&self) -> RangeInclusive<Idx>ⓘNotable traits for RangeInclusive<A>impl<A> Iterator for RangeInclusive<A> where
A: Step, type Item = A;
A: Step, type Item = A;
sourceimpl Clone for VarBlake2b
impl Clone for VarBlake2b
fn clone(&self) -> VarBlake2b
sourceimpl Clone for VarBlake2s
impl Clone for VarBlake2s
fn clone(&self) -> VarBlake2s
sourceimpl Clone for InvalidOutputSize
impl Clone for InvalidOutputSize
fn clone(&self) -> InvalidOutputSize
impl<T, N> Clone for GenericArray<T, N> where
T: Clone,
N: ArrayLength<T>,
impl<T, N> Clone for GenericArray<T, N> where
T: Clone,
N: ArrayLength<T>,
fn clone(&self) -> GenericArray<T, N>
impl<M> Clone for Output<M> where
M: Clone + Mac,
<M as Mac>::OutputSize: Clone,
impl<M> Clone for Output<M> where
M: Clone + Mac,
<M as Mac>::OutputSize: Clone,
fn clone(&self) -> Output<M>
sourceimpl Clone for G1Projective
impl Clone for G1Projective
fn clone(&self) -> G1Projective
sourceimpl Clone for G2Projective
impl Clone for G2Projective
fn clone(&self) -> G2Projective
sourceimpl<R> Clone for BlockRng<R> where
R: Clone + BlockRngCore + ?Sized,
<R as BlockRngCore>::Results: Clone,
impl<R> Clone for BlockRng<R> where
R: Clone + BlockRngCore + ?Sized,
<R as BlockRngCore>::Results: Clone,
sourceimpl<R> Clone for BlockRng64<R> where
R: Clone + BlockRngCore + ?Sized,
<R as BlockRngCore>::Results: Clone,
impl<R> Clone for BlockRng64<R> where
R: Clone + BlockRngCore + ?Sized,
<R as BlockRngCore>::Results: Clone,
fn clone(&self) -> BlockRng64<R>
impl<Z> Clone for Zeroizing<Z> where
Z: Zeroize + Clone,
impl<Z> Clone for Zeroizing<Z> where
Z: Zeroize + Clone,
fn clone(&self) -> Zeroizing<Z>
fn clone_from(&mut self, source: &Zeroizing<Z>)
sourceimpl<T> Clone for LinkedList<T> where
T: Clone,
impl<T> Clone for LinkedList<T> where
T: Clone,
fn clone(&self) -> LinkedList<T>
fn clone_from(&mut self, other: &LinkedList<T>)
sourceimpl Clone for FromUtf8Error
impl Clone for FromUtf8Error
fn clone(&self) -> FromUtf8Error
1.57.0 · sourceimpl Clone for TryReserveError
impl Clone for TryReserveError
fn clone(&self) -> TryReserveError
sourceimpl<'_, T> Clone for SymmetricDifference<'_, T>
impl<'_, T> Clone for SymmetricDifference<'_, T>
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;
T: Ord, type Item = &'a T;
sourceimpl<T> Clone for IntoIterSorted<T> where
T: Clone,
impl<T> Clone for IntoIterSorted<T> where
T: Clone,
fn clone(&self) -> IntoIterSorted<T>ⓘNotable traits for IntoIterSorted<T>impl<T> Iterator for IntoIterSorted<T> where
T: Ord, type Item = T;
T: Ord, type Item = T;
sourceimpl Clone for IntoStringError
impl Clone for IntoStringError
fn clone(&self) -> IntoStringError
sourceimpl<'_, T> Clone for Difference<'_, T>
impl<'_, T> Clone for Difference<'_, T>
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;
T: Ord, type Item = &'a T;
sourceimpl Clone for TryReserveErrorKind
impl Clone for TryReserveErrorKind
fn clone(&self) -> TryReserveErrorKind
sourceimpl Clone for FromVecWithNulError
impl Clone for FromVecWithNulError
fn clone(&self) -> FromVecWithNulError
sourceimpl<T, A> Clone for Box<T, A> where
T: Clone,
A: Allocator + Clone,
impl<T, A> Clone for Box<T, A> where
T: Clone,
A: Allocator + Clone,
sourcefn 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;
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;
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
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);
sourcefn clone_from(&mut self, source: &Box<T, A>)
fn clone_from(&mut self, source: &Box<T, A>)
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);
sourceimpl<T> Clone for BinaryHeap<T> where
T: Clone,
impl<T> Clone for BinaryHeap<T> where
T: Clone,
fn clone(&self) -> BinaryHeap<T>
fn clone_from(&mut self, source: &BinaryHeap<T>)
sourceimpl<'_, T> Clone for Intersection<'_, T>
impl<'_, T> Clone for Intersection<'_, T>
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;
T: Ord, type Item = &'a T;
sourceimpl<'a> Clone for Unexpected<'a>
impl<'a> Clone for Unexpected<'a>
fn clone(&self) -> Unexpected<'a>
sourceimpl<E> Clone for U8Deserializer<E>
impl<E> Clone for U8Deserializer<E>
fn clone(&self) -> U8Deserializer<E>
sourceimpl Clone for IgnoredAny
impl Clone for IgnoredAny
fn clone(&self) -> IgnoredAny
sourceimpl<E> Clone for U128Deserializer<E>
impl<E> Clone for U128Deserializer<E>
fn clone(&self) -> U128Deserializer<E>
sourceimpl<E> Clone for F32Deserializer<E>
impl<E> Clone for F32Deserializer<E>
fn clone(&self) -> F32Deserializer<E>
sourceimpl<A> Clone for SeqAccessDeserializer<A> where
A: Clone,
impl<A> Clone for SeqAccessDeserializer<A> where
A: Clone,
fn clone(&self) -> SeqAccessDeserializer<A>
sourceimpl<E> Clone for IsizeDeserializer<E>
impl<E> Clone for IsizeDeserializer<E>
fn clone(&self) -> IsizeDeserializer<E>
sourceimpl<E> Clone for I8Deserializer<E>
impl<E> Clone for I8Deserializer<E>
fn clone(&self) -> I8Deserializer<E>
sourceimpl<E> Clone for I64Deserializer<E>
impl<E> Clone for I64Deserializer<E>
fn clone(&self) -> I64Deserializer<E>
sourceimpl<E> Clone for U32Deserializer<E>
impl<E> Clone for U32Deserializer<E>
fn clone(&self) -> U32Deserializer<E>
sourceimpl<E> Clone for F64Deserializer<E>
impl<E> Clone for F64Deserializer<E>
fn clone(&self) -> F64Deserializer<E>
sourceimpl<'de, I, E> Clone for MapDeserializer<'de, I, E> where
I: Iterator + Clone,
<I as Iterator>::Item: Pair,
<<I as Iterator>::Item as Pair>::Second: Clone,
impl<'de, I, E> Clone for MapDeserializer<'de, I, E> where
I: Iterator + Clone,
<I as Iterator>::Item: Pair,
<<I as Iterator>::Item as Pair>::Second: Clone,
fn clone(&self) -> MapDeserializer<'de, I, E>
sourceimpl<E> Clone for UsizeDeserializer<E>
impl<E> Clone for UsizeDeserializer<E>
fn clone(&self) -> UsizeDeserializer<E>
sourceimpl<A> Clone for MapAccessDeserializer<A> where
A: Clone,
impl<A> Clone for MapAccessDeserializer<A> where
A: Clone,
fn clone(&self) -> MapAccessDeserializer<A>
sourceimpl<'de, E> Clone for BorrowedBytesDeserializer<'de, E>
impl<'de, E> Clone for BorrowedBytesDeserializer<'de, E>
fn clone(&self) -> BorrowedBytesDeserializer<'de, E>
sourceimpl<E> Clone for I32Deserializer<E>
impl<E> Clone for I32Deserializer<E>
fn clone(&self) -> I32Deserializer<E>
sourceimpl<E> Clone for I128Deserializer<E>
impl<E> Clone for I128Deserializer<E>
fn clone(&self) -> I128Deserializer<E>
sourceimpl<E> Clone for U16Deserializer<E>
impl<E> Clone for U16Deserializer<E>
fn clone(&self) -> U16Deserializer<E>
sourceimpl<E> Clone for CharDeserializer<E>
impl<E> Clone for CharDeserializer<E>
fn clone(&self) -> CharDeserializer<E>
sourceimpl<'de, E> Clone for StrDeserializer<'de, E>
impl<'de, E> Clone for StrDeserializer<'de, E>
fn clone(&self) -> StrDeserializer<'de, E>
sourceimpl<E> Clone for BoolDeserializer<E>
impl<E> Clone for BoolDeserializer<E>
fn clone(&self) -> BoolDeserializer<E>
sourceimpl<'a, E> Clone for BytesDeserializer<'a, E>
impl<'a, E> Clone for BytesDeserializer<'a, E>
fn clone(&self) -> BytesDeserializer<'a, E>
sourceimpl<'de, E> Clone for BorrowedStrDeserializer<'de, E>
impl<'de, E> Clone for BorrowedStrDeserializer<'de, E>
fn clone(&self) -> BorrowedStrDeserializer<'de, E>
sourceimpl<I, E> Clone for SeqDeserializer<I, E> where
I: Clone,
E: Clone,
impl<I, E> Clone for SeqDeserializer<I, E> where
I: Clone,
E: Clone,
fn clone(&self) -> SeqDeserializer<I, E>
sourceimpl<E> Clone for I16Deserializer<E>
impl<E> Clone for I16Deserializer<E>
fn clone(&self) -> I16Deserializer<E>
sourceimpl<E> Clone for UnitDeserializer<E>
impl<E> Clone for UnitDeserializer<E>
fn clone(&self) -> UnitDeserializer<E>
sourceimpl<E> Clone for U64Deserializer<E>
impl<E> Clone for U64Deserializer<E>
fn clone(&self) -> U64Deserializer<E>
sourceimpl Clone for LinkedIndexU16
impl Clone for LinkedIndexU16
fn clone(&self) -> LinkedIndexU16
sourceimpl Clone for LinkedIndexU8
impl Clone for LinkedIndexU8
fn clone(&self) -> LinkedIndexU8
sourceimpl<K, V, S, const N: usize> Clone for IndexMap<K, V, S, N> where
K: Eq + Hash + Clone,
V: Clone,
S: Clone,
impl<K, V, S, const N: usize> Clone for IndexMap<K, V, S, N> where
K: Eq + Hash + Clone,
V: Clone,
S: Clone,
sourceimpl Clone for LinkedIndexUsize
impl Clone for LinkedIndexUsize
fn clone(&self) -> LinkedIndexUsize
sourceimpl<'a, T, const N: usize> Clone for OldestOrdered<'a, T, N> where
T: Clone,
impl<'a, T, const N: usize> Clone for OldestOrdered<'a, T, N> where
T: Clone,
fn clone(&self) -> OldestOrdered<'a, T, N>ⓘNotable traits for OldestOrdered<'a, T, N>impl<'a, T, const N: usize> Iterator for OldestOrdered<'a, T, N> type Item = &'a T;
sourceimpl<T, K, const N: usize> Clone for BinaryHeap<T, K, N> where
K: Kind,
T: Ord + Clone,
impl<T, K, const N: usize> Clone for BinaryHeap<T, K, N> where
K: Kind,
T: Ord + Clone,
fn clone(&self) -> BinaryHeap<T, K, N>
impl<H> Clone for BuildHasherDefault<H> where
H: Default + Hasher,
impl<H> Clone for BuildHasherDefault<H> where
H: Default + Hasher,
fn clone(&self) -> BuildHasherDefault<H>
sourceimpl<'_, T, S, A> Clone for SymmetricDifference<'_, T, S, A> where
A: Allocator + Clone,
impl<'_, T, S, A> Clone for SymmetricDifference<'_, T, S, A> where
A: Allocator + Clone,
fn clone(&self) -> SymmetricDifference<'_, T, S, A>ⓘNotable traits for SymmetricDifference<'a, T, S, A>impl<'a, T, S, A> Iterator for SymmetricDifference<'a, T, S, A> where
T: Eq + Hash,
S: BuildHasher,
A: Allocator + Clone, type Item = &'a T;
T: Eq + Hash,
S: BuildHasher,
A: Allocator + Clone, type Item = &'a T;
sourceimpl Clone for TryReserveError
impl Clone for TryReserveError
fn clone(&self) -> TryReserveError
sourceimpl<'_, T, S, A> Clone for Difference<'_, T, S, A> where
A: Allocator + Clone,
impl<'_, T, S, A> Clone for Difference<'_, T, S, A> where
A: Allocator + Clone,
fn clone(&self) -> Difference<'_, T, S, A>ⓘNotable traits for Difference<'a, T, S, A>impl<'a, T, S, A> Iterator for Difference<'a, T, S, A> where
T: Eq + Hash,
S: BuildHasher,
A: Allocator + Clone, type Item = &'a T;
T: Eq + Hash,
S: BuildHasher,
A: Allocator + Clone, type Item = &'a T;
sourceimpl<'_, T, S, A> Clone for Intersection<'_, T, S, A> where
A: Allocator + Clone,
impl<'_, T, S, A> Clone for Intersection<'_, T, S, A> where
A: Allocator + Clone,
fn clone(&self) -> Intersection<'_, T, S, A>ⓘNotable traits for Intersection<'a, T, S, A>impl<'a, T, S, A> Iterator for Intersection<'a, T, S, A> where
T: Eq + Hash,
S: BuildHasher,
A: Allocator + Clone, type Item = &'a T;
T: Eq + Hash,
S: BuildHasher,
A: Allocator + Clone, type Item = &'a T;