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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

Closes #141138

The change explicitly explains that cloning behavior varies by type and clarifies that smart pointers (Arc, Rc) share the same underlying data. I've also added an example of cloning to Arc.

r? @fee1-dead

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 18, 2025
@fee1-dead
Copy link
Member

My role (T-compiler) does not involve approving libs api docs, sorry.

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 19, 2025
@rustbot rustbot assigned m-ou-se and unassigned fee1-dead May 19, 2025
@fee1-dead fee1-dead removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 19, 2025
@@ -38,7 +38,25 @@

mod uninit;

/// A common trait for the ability to explicitly duplicate an object.
/// A common trait that allows explicit creation of a "duplicate" value.
Copy link
Member

Choose a reason for hiding this comment

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

No need for scare quotes. The value is in fact duplicated.

Suggested change
/// A common trait that allows explicit creation of a "duplicate" value.
/// A common trait that allows explicit creation of a duplicate value.

Comment on lines 43 to 44
/// ## Important Note on "Duplication"
///
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this heading adds much? At worst, it's a little confusing due to what else gets moved under it.

Suggested change
/// ## Important Note on "Duplication"
///

Comment on lines 45 to 52
/// What "duplication" means depends on the type implementing `Clone`:
///
/// - For most types, calling [`clone`] creates a semantically independent copy with its own
/// unique identity and state.
/// - For smart pointers (like [`Arc`], [`Rc`]), [`clone`] increases the reference count but points
/// to the same underlying data. This means modifications to the underlying data through one
/// clone will be visible through other clones.
/// - For reference types (`&T`), [`clone`] just creates another reference to the same value.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that the behavior actually varies much. What is duplicated is the value.

However, some types indeed, as values, are instead references to other data. So you get a new reference. It might be more concise to simply state that you always get a new value, it's just not guaranteed that the new value does not contain references to other data that is not duplicated.

Copy link
Member

Choose a reason for hiding this comment

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

Besides, this part of the text is just repeated on the docs for Clone::clone itself.

Comment on lines 58 to 59
/// [`Arc`]: ../../std/sync/struct.Arc.html
/// [`Rc`]: ../../std/rc/struct.Rc.html
Copy link
Member

Choose a reason for hiding this comment

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

After this we start talking about how this differs from Copy, you see, and that probably doesn't fit under the same heading.

@workingjubilee
Copy link
Member

This is very not an API change so anyone on libs can take it.

yoink

r? @workingjubilee

@rustbot rustbot assigned workingjubilee and unassigned m-ou-se May 30, 2025
@workingjubilee
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 30, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This commit improves the Clone trait documentation to address confusion
around what "duplication" means for different types, especially for smart
pointers like Arc<Mutex<T>>.

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

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

@rustbot ready

Comment on lines +43 to +46
/// 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.
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.

Comment on lines +159 to +168
/// 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
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?

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clone documentation can be confusing to beginners around "duplication" language
5 participants