1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The `Clone` trait for types that cannot be 'implicitly copied'.
13 //! In Rust, some simple types are "implicitly copyable" and when you
14 //! assign them or pass them as arguments, the receiver will get a copy,
15 //! leaving the original value in place. These types do not require
16 //! allocation to copy and do not have finalizers (i.e. they do not
17 //! contain owned boxes or implement [`Drop`]), so the compiler considers
18 //! them cheap and safe to copy. For other types copies must be made
19 //! explicitly, by convention implementing the [`Clone`] trait and calling
20 //! the [`clone`][clone] method.
22 //! [`Clone`]: trait.Clone.html
23 //! [clone]: trait.Clone.html#tymethod.clone
24 //! [`Drop`]: ../../std/ops/trait.Drop.html
26 //! Basic usage example:
29 //! let s = String::new(); // String type implements Clone
30 //! let copy = s.clone(); // so we can clone it
33 //! To easily implement the Clone trait, you can also use
34 //! `#[derive(Clone)]`. Example:
37 //! #[derive(Clone)] // we add the Clone trait to Morpheus struct
44 //! let f = Morpheus { blue_pill: 0.0, red_pill: 0 };
45 //! let copy = f.clone(); // and now we can clone it!
49 #![stable(feature = "rust1", since = "1.0.0")]
51 /// A common trait for the ability to explicitly duplicate an object.
53 /// Differs from [`Copy`] in that [`Copy`] is implicit and extremely inexpensive, while
54 /// `Clone` is always explicit and may or may not be expensive. In order to enforce
55 /// these characteristics, Rust does not allow you to reimplement [`Copy`], but you
56 /// may reimplement `Clone` and run arbitrary code.
58 /// Since `Clone` is more general than [`Copy`], you can automatically make anything
59 /// [`Copy`] be `Clone` as well.
63 /// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
64 /// implementation of [`clone`] calls [`clone`] on each field.
68 /// Closure types automatically implement `Clone` if they capture no value from the environment
69 /// or if all such captured values implement `Clone` themselves.
71 /// ## How can I implement `Clone`?
73 /// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:
74 /// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`.
75 /// Manual implementations should be careful to uphold this invariant; however, unsafe code
76 /// must not rely on it to ensure memory safety.
78 /// An example is an array holding more than 32 elements of a type that is `Clone`; the standard
79 /// library only implements `Clone` up until arrays of size 32. In this case, the implementation of
80 /// `Clone` cannot be `derive`d, but can be implemented as:
82 /// [`Copy`]: ../../std/marker/trait.Copy.html
83 /// [`clone`]: trait.Clone.html#tymethod.clone
88 /// frequencies: [i32; 100],
91 /// impl Clone for Stats {
92 /// fn clone(&self) -> Stats { *self }
95 #[stable(feature = "rust1", since = "1.0.0")]
97 pub trait Clone : Sized {
98 /// Returns a copy of the value.
103 /// let hello = "Hello"; // &str implements Clone
105 /// assert_eq!("Hello", hello.clone());
107 #[stable(feature = "rust1", since = "1.0.0")]
108 #[must_use = "cloning is often expensive and is not expected to have side effects"]
109 fn clone(&self) -> Self;
111 /// Performs copy-assignment from `source`.
113 /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
114 /// but can be overridden to reuse the resources of `a` to avoid unnecessary
117 #[stable(feature = "rust1", since = "1.0.0")]
118 fn clone_from(&mut self, source: &Self) {
119 *self = source.clone()
123 // FIXME(aburka): these structs are used solely by #[derive] to
124 // assert that every component of a type implements Clone or Copy.
126 // These structs should never appear in user code.
128 #[allow(missing_debug_implementations)]
129 #[unstable(feature = "derive_clone_copy",
130 reason = "deriving hack, should not be public",
132 pub struct AssertParamIsClone<T: Clone + ?Sized> { _field: ::marker::PhantomData<T> }
134 #[allow(missing_debug_implementations)]
135 #[unstable(feature = "derive_clone_copy",
136 reason = "deriving hack, should not be public",
138 pub struct AssertParamIsCopy<T: Copy + ?Sized> { _field: ::marker::PhantomData<T> }