3838
3939mod uninit;
4040
41- /// A common trait for the ability to explicitly duplicate an object.
41+ /// A common trait that allows explicit creation of a "duplicate" value.
42+ ///
43+ /// ## Important Note on "Duplication"
44+ ///
45+ /// What "duplication" means depends on the type implementing `Clone`:
46+ ///
47+ /// - For most types, calling [`clone`] creates a semantically independent copy with its own
48+ /// unique identity and state.
49+ /// - For smart pointers (like [`Arc`], [`Rc`]), [`clone`] increases the reference count but points
50+ /// to the same underlying data. This means modifications to the underlying data through one
51+ /// clone will be visible through other clones.
52+ /// - For reference types (`&T`), [`clone`] just creates another reference to the same value.
53+ ///
54+ /// This distinction is especially important when using `#[derive(Clone)]` on structs containing
55+ /// smart pointers like `Arc<Mutex<T>>` - the cloned struct will share mutable state with the
56+ /// original.
57+ ///
58+ /// [`Arc`]: ../../std/sync/struct.Arc.html
59+ /// [`Rc`]: ../../std/rc/struct.Rc.html
4260///
4361/// Differs from [`Copy`] in that [`Copy`] is implicit and an inexpensive bit-wise copy, while
4462/// `Clone` is always explicit and may or may not be expensive. In order to enforce
@@ -147,7 +165,16 @@ mod uninit;
147165#[ rustc_diagnostic_item = "Clone" ]
148166#[ rustc_trivial_field_reads]
149167pub trait Clone : Sized {
150- /// Returns a copy of the value.
168+ /// Returns a duplicate of the value.
169+ ///
170+ /// Note that what "duplicate" means varies by type:
171+ /// - For most types, this creates a deep, independent copy
172+ /// - For reference types like `&T`, this creates another reference to the same value
173+ /// - For smart pointers like [`Arc`] or [`Rc`], this increments the reference count
174+ /// but still points to the same underlying data
175+ ///
176+ /// [`Arc`]: ../../std/sync/struct.Arc.html
177+ /// [`Rc`]: ../../std/rc/struct.Rc.html
151178 ///
152179 /// # Examples
153180 ///
@@ -157,6 +184,23 @@ pub trait Clone: Sized {
157184 ///
158185 /// assert_eq!("Hello", hello.clone());
159186 /// ```
187+ ///
188+ /// Example with a reference-counted type:
189+ ///
190+ /// ```
191+ /// use std::sync::{Arc, Mutex};
192+ ///
193+ /// let data = Arc::new(Mutex::new(vec![1, 2, 3]));
194+ /// let data_clone = data.clone(); // Creates another Arc pointing to the same Mutex
195+ ///
196+ /// {
197+ /// let mut lock = data.lock().unwrap();
198+ /// lock.push(4);
199+ /// }
200+ ///
201+ /// // Changes are visible through the clone because they share the same underlying data
202+ /// assert_eq!(*data_clone.lock().unwrap(), vec![1, 2, 3, 4]);
203+ /// ```
160204 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
161205 #[ must_use = "cloning is often expensive and is not expected to have side effects" ]
162206 // Clone::clone is special because the compiler generates MIR to implement it for some types.
0 commit comments