core::clone::Clone - Rust
Description: API documentation for the Rust `Clone` trait in crate `core`.
Keywords: rust, rustlang, rust-lang, Clone

# Trait core::clone::Clone 1.0.0 [−] [src]

    #[lang = "clone"]
    pub trait Clone: Sized {
        fn clone(&self) -> Self;

        fn clone_from(&mut self, source: &Self) { ... }
    }

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.

## 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 an array holding more than 32 elements of a type that is `Clone`; the standard library only implements `Clone` up until arrays of size 32. In this case, the implementation of `Clone` cannot be `derive`d, but can be implemented as:

    #[derive(Copy)]
    struct Stats {
       frequencies: [i32; 100],
    }

    impl Clone for Stats {
        fn clone(&self) -> Stats { *self }
    }Run

## Required Methods

### `fn clone(&self) -> Self`

Returns a copy of the value.

# Examples

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

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

## Provided Methods

### `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.

## Implementors

- [src]
  `impl Clone for ParseFloatError`
- [src]
  `impl<T: Clone> Clone for Wrapping<T>`
- [src]
  `impl Clone for FpCategory`
- [src]
  `impl Clone for TryFromIntError`
- [src]
  `impl Clone for ParseIntError`
- [src]
  `impl<T> Clone for Discriminant<T>`
- [src]
  `impl<T: Clone> Clone for ManuallyDrop<T>`
- [src]
  `impl<T: Clone + Zeroable> Clone for NonZero<T>`
- [src]
  `impl<T: ?Sized> Clone for Unique<T>`
- [src]
  `impl<T: ?Sized> Clone for Shared<T>`
- [src]
  `impl<T: ?Sized> Clone for PhantomData<T>`
- [src]
  `impl<Y: Clone, R: Clone> Clone for GeneratorState<Y, R>`
- [src]
  `impl Clone for RangeFull`
- [src]
  `impl<Idx: Clone> Clone for Range<Idx>`
- [src]
  `impl<Idx: Clone> Clone for RangeFrom<Idx>`
- [src]
  `impl<Idx: Clone> Clone for RangeTo<Idx>`
- [src]
  `impl<Idx: Clone> Clone for RangeInclusive<Idx>`
- [src]
  `impl<Idx: Clone> Clone for RangeToInclusive<Idx>`
- [src]
  `impl Clone for core::cmp::Ordering`
- [src]
  `impl Clone for Infallible`
- [src]
  `impl Clone for TypeId`
- [src]
  `impl Clone for core::sync::atomic::Ordering`
- [src]
  `impl<T: Copy> Clone for Cell<T>`
- [src]
  `impl<T: Clone> Clone for RefCell<T>`
- [src]
  `impl Clone for ParseCharError`
- [src]
  `impl Clone for CharTryFromError`
- [src]
  `impl Clone for EscapeUnicode`
- [src]
  `impl Clone for EscapeDefault`
- [src]
  `impl Clone for EscapeDebug`
- [src]
  `impl<I: Clone + Iterator<Item = u8>> Clone for DecodeUtf8<I>`
- [src]
  `impl<A: Clone> Clone for Repeat<A>`
- [src]
  `impl<T> Clone for Empty<T>`
- [src]
  `impl<T: Clone> Clone for Once<T>`
- [src]
  `impl<T: Clone> Clone for Rev<T>`
- [src]
  `impl<I: Clone> Clone for Cloned<I>`
- [src]
  `impl<I: Clone> Clone for Cycle<I>`
- [src]
  `impl<I: Clone> Clone for StepBy<I>`
- [src]
  `impl<A: Clone, B: Clone> Clone for Chain<A, B>`
- [src]
  `impl<A: Clone, B: Clone> Clone for Zip<A, B>`
- [src]
  `impl<I: Clone, F: Clone> Clone for Map<I, F>`
- [src]
  `impl<I: Clone, P: Clone> Clone for Filter<I, P>`
- [src]
  `impl<I: Clone, F: Clone> Clone for FilterMap<I, F>`
- [src]
  `impl<I: Clone> Clone for Enumerate<I>`
- [src]
  `impl<I: Clone + Iterator> Clone for Peekable<I> where
  I::Item: Clone,`
- [src]
  `impl<I: Clone, P: Clone> Clone for SkipWhile<I, P>`
- [src]
  `impl<I: Clone, P: Clone> Clone for TakeWhile<I, P>`
- [src]
  `impl<I: Clone> Clone for Skip<I>`
- [src]
  `impl<I: Clone> Clone for Take<I>`
- [src]
  `impl<I: Clone, St: Clone, F: Clone> Clone for Scan<I, St, F>`
- [src]
  `impl<I: Clone, U: Clone + IntoIterator, F: Clone> Clone for FlatMap<I, U, F> where
  U::IntoIter: Clone,
  U::IntoIter: Clone,`
- [src]
  `impl<I: Clone> Clone for Fuse<I>`
- [src]
  `impl<I: Clone, F: Clone> Clone for Inspect<I, F>`
- [src]
  `impl<T: Clone> Clone for Option<T>`
- [src]
  `impl<'a, A> Clone for core::option::Iter<'a, A>`
- [src]
  `impl<A: Clone> Clone for IntoIter<A>`
- [src]
  `impl Clone for NoneError`
- [src]
  `impl Clone for TraitObject`
- [src]
  `impl<T: Clone, E: Clone> Clone for Result<T, E>`
- [src]
  `impl<'a, T> Clone for core::result::Iter<'a, T>`
- [src]
  `impl<'a, T> Clone for core::slice::Iter<'a, T>`
- [src]
  `impl<'a, T, P> Clone for core::slice::Split<'a, T, P> where
  P: Clone + FnMut(&T) -> bool,`
- [src]
  `impl<'a, T: Clone + 'a, P: Clone> Clone for core::slice::RSplit<'a, T, P> where
  P: FnMut(&T) -> bool,`
- [src]
  `impl<'a, T> Clone for Windows<'a, T>`
- [src]
  `impl<'a, T> Clone for Chunks<'a, T>`
- [src]
  `impl Clone for SearchStep`
- [src]
  `impl<'a> Clone for CharSearcher<'a>`
- [src]
  `impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>`
- [src]
  `impl<'a, F: Clone> Clone for CharPredicateSearcher<'a, F> where
  F: FnMut(char) -> bool,`
- [src]
  `impl<'a, 'b> Clone for StrSearcher<'a, 'b>`
- [src]
  `impl Clone for ParseBoolError`
- [src]
  `impl Clone for Utf8Error`
- [src]
  `impl<'a> Clone for Chars<'a>`
- [src]
  `impl<'a> Clone for CharIndices<'a>`
- [src]
  `impl<'a> Clone for Bytes<'a>`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for core::str::Split<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for core::str::RSplit<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for SplitTerminator<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for RSplitTerminator<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for SplitN<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for RSplitN<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for MatchIndices<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for RMatchIndices<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for Matches<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a, P: Pattern<'a>> Clone for RMatches<'a, P> where
  P::Searcher: Clone,`
- [src]
  `impl<'a> Clone for Lines<'a>`
- [src]
  `impl<'a> Clone for LinesAny<'a>`
- [src]
  `impl Clone for SipHasher13`
- [src]
  `impl Clone for SipHasher24`
- [src]
  `impl Clone for SipHasher`
- [src]
  `impl<H> Clone for BuildHasherDefault<H>`
- [src]
  `impl Clone for Error`
- [src]
  `impl<'a> Clone for Arguments<'a>`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)