Skip to content

std: clarify Clone trait documentation about duplication semantics #141215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions library/core/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@

mod uninit;

/// A common trait for the ability to explicitly duplicate an object.
/// A common trait that allows explicit creation of a duplicate value.
///
/// Calling [`clone`] always produces a new value.
/// However, for types that are references to other data (such as smart pointers or references),
/// the new value may still point to the same underlying data, rather than duplicating it.
/// See [`Clone::clone`] for more details.
Comment on lines +43 to +46
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't categorize it here, I'm just reminding the user of the situation.

///
/// This distinction is especially important when using `#[derive(Clone)]` on structs containing
/// smart pointers like `Arc<Mutex<T>>` - the cloned struct will share mutable state with the
/// original.
///
/// 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
Expand Down Expand Up @@ -147,7 +156,16 @@ mod uninit;
#[rustc_diagnostic_item = "Clone"]
#[rustc_trivial_field_reads]
pub trait Clone: Sized {
/// Returns a copy of the value.
/// Returns a duplicate of the value.
///
/// Note that what "duplicate" means varies by type:
/// - For most types, this creates a deep, independent copy
/// - For reference types like `&T`, this creates another reference to the same value
/// - For smart pointers like [`Arc`] or [`Rc`], this increments the reference count
/// but still points to the same underlying data
///
/// [`Arc`]: ../../std/sync/struct.Arc.html
/// [`Rc`]: ../../std/rc/struct.Rc.html
Comment on lines +159 to +168
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to change it here, also similar to Trait Clone?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I think the summary list actually fits better here. Thanks!

///
/// # Examples
///
Expand All @@ -157,6 +175,23 @@ pub trait Clone: Sized {
///
/// assert_eq!("Hello", hello.clone());
/// ```
///
/// Example with a reference-counted type:
///
/// ```
/// use std::sync::{Arc, Mutex};
///
/// let data = Arc::new(Mutex::new(vec![1, 2, 3]));
/// let data_clone = data.clone(); // Creates another Arc pointing to the same Mutex
///
/// {
/// let mut lock = data.lock().unwrap();
/// lock.push(4);
/// }
///
/// // Changes are visible through the clone because they share the same underlying data
/// assert_eq!(*data_clone.lock().unwrap(), vec![1, 2, 3, 4]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "cloning is often expensive and is not expected to have side effects"]
// Clone::clone is special because the compiler generates MIR to implement it for some types.
Expand Down
Loading