no_std_compat::clone

Trait Clone

1.0.0 · Source
pub trait Clone: Sized {
    // Required method
    fn clone(&self) -> Self;

    // Provided method
    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 derived 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 derived, 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
    }
}

If we derive:

#[derive(Copy, Clone)]
struct Generate<T>(fn() -> T);

the auto-derived implementations will have unnecessary T: Copy and T: Clone bounds:


// Automatically derived
impl<T: Copy> Copy for Generate<T> { }

// Automatically derived
impl<T: Clone> Clone for Generate<T> {
    fn clone(&self) -> Generate<T> {
        Generate(Clone::clone(&self.0))
    }
}

The bounds are unnecessary because clearly the function itself should be copy- and cloneable even if its return type is not:

#[derive(Copy, Clone)]
struct Generate<T>(fn() -> T);

struct NotCloneable;

fn generate_not_cloneable() -> NotCloneable {
    NotCloneable
}

Generate(generate_not_cloneable).clone(); // error: trait bounds were not satisfied
// Note: With the manual implementations the above line will compile.

§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)
  • 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 implement Clone (even if the referent doesn’t), while variables captured by mutable reference never implement Clone.

Required Methods§

1.0.0 · Source

fn clone(&self) -> Self

Returns a copy of the value.

§Examples
let hello = "Hello"; // &str implements Clone

assert_eq!("Hello", hello.clone());

Provided Methods§

1.0.0 · Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Clone for AsciiChar

1.0.0 · Source§

impl Clone for no_std_compat::cmp::Ordering

1.34.0 · Source§

impl Clone for Infallible

1.28.0 · Source§

impl Clone for no_std_compat::fmt::Alignment

1.0.0 · Source§

impl Clone for FpCategory

1.55.0 · Source§

impl Clone for IntErrorKind

Source§

impl Clone for SearchStep

1.0.0 · Source§

impl Clone for no_std_compat::sync::atomic::Ordering

1.7.0 · Source§

impl Clone for IpAddr

Source§

impl Clone for Ipv6MulticastScope

1.0.0 · Source§

impl Clone for SocketAddr

1.0.0 · Source§

impl Clone for bool

1.0.0 · Source§

impl Clone for char

1.0.0 · Source§

impl Clone for f16

1.0.0 · Source§

impl Clone for f32

1.0.0 · Source§

impl Clone for f64

1.0.0 · Source§

impl Clone for f128

1.0.0 · Source§

impl Clone for i8

1.0.0 · Source§

impl Clone for i16

1.0.0 · Source§

impl Clone for i32

1.0.0 · Source§

impl Clone for i64

1.0.0 · Source§

impl Clone for i128

1.0.0 · Source§

impl Clone for isize

Source§

impl Clone for !

1.0.0 · Source§

impl Clone for u8

1.0.0 · Source§

impl Clone for u16

1.0.0 · Source§

impl Clone for u32

1.0.0 · Source§

impl Clone for u64

1.0.0 · Source§

impl Clone for u128

1.0.0 · Source§

impl Clone for usize

Source§

impl Clone for AllocError

1.28.0 · Source§

impl Clone for Layout

1.50.0 · Source§

impl Clone for LayoutError

1.0.0 · Source§

impl Clone for TypeId

1.27.0 · Source§

impl Clone for CpuidResult

1.27.0 · Source§

impl Clone for __m128

Source§

impl Clone for __m128bh

1.27.0 · Source§

impl Clone for __m128d

Source§

impl Clone for __m128h

1.27.0 · Source§

impl Clone for __m128i

1.27.0 · Source§

impl Clone for __m256

Source§

impl Clone for __m256bh

1.27.0 · Source§

impl Clone for __m256d

Source§

impl Clone for __m256h

1.27.0 · Source§

impl Clone for __m256i

1.72.0 · Source§

impl Clone for __m512

Source§

impl Clone for __m512bh

1.72.0 · Source§

impl Clone for __m512d

Source§

impl Clone for __m512h

1.72.0 · Source§

impl Clone for __m512i

Source§

impl Clone for bf16

1.34.0 · Source§

impl Clone for TryFromSliceError

1.0.0 · Source§

impl Clone for no_std_compat::ascii::EscapeDefault

1.34.0 · Source§

impl Clone for CharTryFromError

1.9.0 · Source§

impl Clone for DecodeUtf16Error

1.20.0 · Source§

impl Clone for no_std_compat::char::EscapeDebug

1.0.0 · Source§

impl Clone for no_std_compat::char::EscapeDefault

1.0.0 · Source§

impl Clone for no_std_compat::char::EscapeUnicode

1.20.0 · Source§

impl Clone for ParseCharError

1.0.0 · Source§

impl Clone for ToLowercase

1.0.0 · Source§

impl Clone for ToUppercase

1.59.0 · Source§

impl Clone for TryFromCharError

1.69.0 · Source§

impl Clone for FromBytesUntilNulError

1.64.0 · Source§

impl Clone for FromBytesWithNulError

1.0.0 · Source§

impl Clone for Error

1.0.0 · Source§

impl Clone for SipHasher

1.33.0 · Source§

impl Clone for PhantomPinned

Source§

impl Clone for Assume

1.0.0 · Source§

impl Clone for ParseFloatError

1.0.0 · Source§

impl Clone for ParseIntError

1.34.0 · Source§

impl Clone for TryFromIntError

1.0.0 · Source§

impl Clone for RangeFull

Source§

impl Clone for no_std_compat::ptr::Alignment

1.0.0 · Source§

impl Clone for ParseBoolError

1.0.0 · Source§

impl Clone for Utf8Error

Source§

impl Clone for LocalWaker

1.36.0 · Source§

impl Clone for RawWakerVTable

1.36.0 · Source§

impl Clone for Waker

1.3.0 · Source§

impl Clone for Duration

1.66.0 · Source§

impl Clone for TryFromFloatSecsError

1.0.0 · Source§

impl Clone for Ipv4Addr

1.0.0 · Source§

impl Clone for Ipv6Addr

1.0.0 · Source§

impl Clone for AddrParseError

1.0.0 · Source§

impl Clone for SocketAddrV4

1.0.0 · Source§

impl Clone for SocketAddrV6

Source§

impl<'a> Clone for Utf8Pattern<'a>

Source§

impl<'a> Clone for no_std_compat::ffi::c_str::Bytes<'a>

1.0.0 · Source§

impl<'a> Clone for Arguments<'a>

1.60.0 · Source§

impl<'a> Clone for EscapeAscii<'a>

Source§

impl<'a> Clone for CharSearcher<'a>

1.0.0 · Source§

impl<'a> Clone for no_std_compat::str::Bytes<'a>

1.0.0 · Source§

impl<'a> Clone for CharIndices<'a>

1.0.0 · Source§

impl<'a> Clone for Chars<'a>

1.8.0 · Source§

impl<'a> Clone for EncodeUtf16<'a>

1.34.0 · Source§

impl<'a> Clone for no_std_compat::str::EscapeDebug<'a>

1.34.0 · Source§

impl<'a> Clone for no_std_compat::str::EscapeDefault<'a>

1.34.0 · Source§

impl<'a> Clone for no_std_compat::str::EscapeUnicode<'a>

1.0.0 · Source§

impl<'a> Clone for Lines<'a>

1.0.0 · Source§

impl<'a> Clone for LinesAny<'a>

1.34.0 · Source§

impl<'a> Clone for SplitAsciiWhitespace<'a>

1.1.0 · Source§

impl<'a> Clone for SplitWhitespace<'a>

1.79.0 · Source§

impl<'a> Clone for Utf8Chunk<'a>

1.79.0 · Source§

impl<'a> Clone for Utf8Chunks<'a>

Source§

impl<'a> Clone for Source<'a>

1.10.0 · Source§

impl<'a> Clone for Location<'a>

Source§

impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>

Source§

impl<'a, 'b> Clone for StrSearcher<'a, 'b>

Source§

impl<'a, 'b, const N: usize> Clone for CharArrayRefSearcher<'a, 'b, N>

Source§

impl<'a, F> Clone for CharPredicateSearcher<'a, F>
where F: Clone + FnMut(char) -> bool,

1.5.0 · Source§

impl<'a, P> Clone for MatchIndices<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.2.0 · Source§

impl<'a, P> Clone for Matches<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.5.0 · Source§

impl<'a, P> Clone for RMatchIndices<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.2.0 · Source§

impl<'a, P> Clone for RMatches<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.0.0 · Source§

impl<'a, P> Clone for no_std_compat::str::RSplit<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.0.0 · Source§

impl<'a, P> Clone for RSplitN<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.0.0 · Source§

impl<'a, P> Clone for RSplitTerminator<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.0.0 · Source§

impl<'a, P> Clone for no_std_compat::str::Split<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.51.0 · Source§

impl<'a, P> Clone for no_std_compat::str::SplitInclusive<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.0.0 · Source§

impl<'a, P> Clone for SplitN<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.0.0 · Source§

impl<'a, P> Clone for SplitTerminator<'a, P>
where P: Pattern, <P as Pattern>::Searcher<'a>: Clone,

1.31.0 · Source§

impl<'a, T> Clone for RChunksExact<'a, T>

Source§

impl<'a, T, const N: usize> Clone for ArrayWindows<'a, T, N>
where T: Clone + 'a,

Source§

impl<'a, const N: usize> Clone for CharArraySearcher<'a, N>

Source§

impl<'f> Clone for VaListImpl<'f>

1.0.0 · Source§

impl<A> Clone for Repeat<A>
where A: Clone,

1.82.0 · Source§

impl<A> Clone for RepeatN<A>
where A: Clone,

1.0.0 · Source§

impl<A> Clone for no_std_compat::option::IntoIter<A>
where A: Clone,

1.0.0 · Source§

impl<A> Clone for no_std_compat::option::Iter<'_, A>

Source§

impl<A> Clone for IterRange<A>
where A: Clone,

Source§

impl<A> Clone for IterRangeFrom<A>
where A: Clone,

Source§

impl<A> Clone for IterRangeInclusive<A>
where A: Clone,

1.0.0 · Source§

impl<A, B> Clone for Chain<A, B>
where A: Clone, B: Clone,

1.0.0 · Source§

impl<A, B> Clone for Zip<A, B>
where A: Clone, B: Clone,

1.55.0 · Source§

impl<B, C> Clone for ControlFlow<B, C>
where B: Clone, C: Clone,

Source§

impl<Dyn> Clone for DynMetadata<Dyn>
where Dyn: ?Sized,

1.34.0 · Source§

impl<F> Clone for FromFn<F>
where F: Clone,

1.43.0 · Source§

impl<F> Clone for OnceWith<F>
where F: Clone,

1.28.0 · Source§

impl<F> Clone for RepeatWith<F>
where F: Clone,

1.7.0 · Source§

impl<H> Clone for BuildHasherDefault<H>

1.9.0 · Source§

impl<I> Clone for DecodeUtf16<I>
where I: Clone + Iterator<Item = u16>,

1.1.0 · Source§

impl<I> Clone for Cloned<I>
where I: Clone,

1.36.0 · Source§

impl<I> Clone for Copied<I>
where I: Clone,

1.0.0 · Source§

impl<I> Clone for Cycle<I>
where I: Clone,

1.0.0 · Source§

impl<I> Clone for Enumerate<I>
where I: Clone,

1.0.0 · Source§

impl<I> Clone for Fuse<I>
where I: Clone,

Source§

impl<I> Clone for Intersperse<I>
where I: Clone + Iterator, <I as Iterator>::Item: Clone,

1.0.0 · Source§

impl<I> Clone for Peekable<I>
where I: Clone + Iterator, <I as Iterator>::Item: Clone,

1.0.0 · Source§

impl<I> Clone for Skip<I>
where I: Clone,

1.28.0 · Source§

impl<I> Clone for StepBy<I>
where I: Clone,

1.0.0 · Source§

impl<I> Clone for Take<I>
where I: Clone,

Source§

impl<I> Clone for FromIter<I>
where I: Clone,

1.0.0 · Source§

impl<I, F> Clone for FilterMap<I, F>
where I: Clone, F: Clone,

1.0.0 · Source§

impl<I, F> Clone for Inspect<I, F>
where I: Clone, F: Clone,

1.0.0 · Source§

impl<I, F> Clone for Map<I, F>
where I: Clone, F: Clone,

Source§

impl<I, F, const N: usize> Clone for MapWindows<I, F, N>
where I: Iterator + Clone, F: Clone, <I as Iterator>::Item: Clone,

Source§

impl<I, G> Clone for IntersperseWith<I, G>
where I: Iterator + Clone, <I as Iterator>::Item: Clone, G: Clone,

1.0.0 · Source§

impl<I, P> Clone for Filter<I, P>
where I: Clone, P: Clone,

1.57.0 · Source§

impl<I, P> Clone for MapWhile<I, P>
where I: Clone, P: Clone,

1.0.0 · Source§

impl<I, P> Clone for SkipWhile<I, P>
where I: Clone, P: Clone,

1.0.0 · Source§

impl<I, P> Clone for TakeWhile<I, P>
where I: Clone, P: Clone,

1.0.0 · Source§

impl<I, St, F> Clone for Scan<I, St, F>
where I: Clone, St: Clone, F: Clone,

1.29.0 · Source§

impl<I, U> Clone for Flatten<I>
where I: Clone + Iterator, <I as Iterator>::Item: IntoIterator<IntoIter = U, Item = <U as Iterator>::Item>, U: Clone + Iterator,

1.0.0 · Source§

impl<I, U, F> Clone for FlatMap<I, U, F>
where I: Clone, F: Clone, U: Clone + IntoIterator, <U as IntoIterator>::IntoIter: Clone,

Source§

impl<I, const N: usize> Clone for no_std_compat::iter::ArrayChunks<I, N>
where I: Clone + Iterator, <I as Iterator>::Item: Clone,

1.0.0 · Source§

impl<Idx> Clone for no_std_compat::ops::Range<Idx>
where Idx: Clone,

1.0.0 · Source§

impl<Idx> Clone for no_std_compat::ops::RangeFrom<Idx>
where Idx: Clone,

1.26.0 · Source§

impl<Idx> Clone for no_std_compat::ops::RangeInclusive<Idx>
where Idx: Clone,

1.0.0 · Source§

impl<Idx> Clone for RangeTo<Idx>
where Idx: Clone,

1.26.0 · Source§

impl<Idx> Clone for RangeToInclusive<Idx>
where Idx: Clone,

Source§

impl<Idx> Clone for core::range::Range<Idx>
where Idx: Clone,

Source§

impl<Idx> Clone for core::range::RangeFrom<Idx>
where Idx: Clone,

Source§

impl<Idx> Clone for core::range::RangeInclusive<Idx>
where Idx: Clone,

1.33.0 · Source§

impl<Ptr> Clone for Pin<Ptr>
where Ptr: Clone,

1.0.0 · Source§

impl<T> !Clone for &mut T
where T: ?Sized,

Shared references can be cloned, but mutable references cannot!

1.17.0 · Source§

impl<T> Clone for Bound<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for Option<T>
where T: Clone,

1.36.0 · Source§

impl<T> Clone for Poll<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for *const T
where T: ?Sized,

1.0.0 · Source§

impl<T> Clone for *mut T
where T: ?Sized,

1.0.0 · Source§

impl<T> Clone for &T
where T: ?Sized,

Shared references can be cloned, but mutable references cannot!

1.0.0 · Source§

impl<T> Clone for Cell<T>
where T: Copy,

1.70.0 · Source§

impl<T> Clone for OnceCell<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for RefCell<T>
where T: Clone,

1.19.0 · Source§

impl<T> Clone for Reverse<T>
where T: Clone,

1.48.0 · Source§

impl<T> Clone for Pending<T>

1.48.0 · Source§

impl<T> Clone for Ready<T>
where T: Clone,

1.2.0 · Source§

impl<T> Clone for Empty<T>

1.2.0 · Source§

impl<T> Clone for Once<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for Rev<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for PhantomData<T>
where T: ?Sized,

1.21.0 · Source§

impl<T> Clone for Discriminant<T>

1.20.0 · Source§

impl<T> Clone for ManuallyDrop<T>
where T: Clone + ?Sized,

1.28.0 · Source§

impl<T> Clone for NonZero<T>

1.74.0 · Source§

impl<T> Clone for Saturating<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for Wrapping<T>
where T: Clone,

1.25.0 · Source§

impl<T> Clone for NonNull<T>
where T: ?Sized,

1.0.0 · Source§

impl<T> Clone for no_std_compat::result::IntoIter<T>
where T: Clone,

1.0.0 · Source§

impl<T> Clone for no_std_compat::result::Iter<'_, T>

1.0.0 · Source§

impl<T> Clone for Chunks<'_, T>

1.31.0 · Source§

impl<T> Clone for ChunksExact<'_, T>

1.0.0 · Source§

impl<T> Clone for no_std_compat::slice::Iter<'_, T>

1.31.0 · Source§

impl<T> Clone for RChunks<'_, T>

1.0.0 · Source§

impl<T> Clone for Windows<'_, T>

1.36.0 · Source§

impl<T> Clone for MaybeUninit<T>
where T: Copy,

1.0.0 · Source§

impl<T, E> Clone for Result<T, E>
where T: Clone, E: Clone,

1.34.0 · Source§

impl<T, F> Clone for Successors<T, F>
where T: Clone, F: Clone,

1.27.0 · Source§

impl<T, P> Clone for no_std_compat::slice::RSplit<'_, T, P>
where P: Clone + FnMut(&T) -> bool,

1.0.0 · Source§

impl<T, P> Clone for no_std_compat::slice::Split<'_, T, P>
where P: Clone + FnMut(&T) -> bool,

1.51.0 · Source§

impl<T, P> Clone for no_std_compat::slice::SplitInclusive<'_, T, P>
where P: Clone + FnMut(&T) -> bool,

1.58.0 · Source§

impl<T, const N: usize> Clone for [T; N]
where T: Clone,

1.40.0 · Source§

impl<T, const N: usize> Clone for no_std_compat::array::IntoIter<T, N>
where T: Clone,

Source§

impl<T, const N: usize> Clone for no_std_compat::slice::ArrayChunks<'_, T, N>

Source§

impl<T, const N: usize> Clone for Mask<T, N>

Source§

impl<T, const N: usize> Clone for Simd<T, N>

Source§

impl<Y, R> Clone for CoroutineState<Y, R>
where Y: Clone, R: Clone,