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

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented May 18, 2025

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.

@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 <[email protected]>
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?

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!

@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
@workingjubilee
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 31, 2025

📌 Commit cea87ec has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 31, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request May 31, 2025
…bilee

std: clarify Clone trait documentation about duplication semantics

Closes rust-lang#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.
bors added a commit that referenced this pull request Jun 1, 2025
Rollup of 6 pull requests

Successful merges:

 - #141072 (Stabilize feature `result_flattening`)
 - #141215 (std: clarify Clone trait documentation about duplication semantics)
 - #141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - #141521 (Add `const` support for float rounding methods)
 - #141812 (Fix "consider borrowing" for else-if)
 - #141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit fa494d6 into rust-lang:master Jun 1, 2025
9 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 1, 2025
rust-timer added a commit that referenced this pull request Jun 1, 2025
Rollup merge of #141215 - xizheyin:issue-141138, r=workingjubilee

std: clarify Clone trait documentation about duplication semantics

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.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 1, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#141072 (Stabilize feature `result_flattening`)
 - rust-lang/rust#141215 (std: clarify Clone trait documentation about duplication semantics)
 - rust-lang/rust#141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - rust-lang/rust#141521 (Add `const` support for float rounding methods)
 - rust-lang/rust#141812 (Fix "consider borrowing" for else-if)
 - rust-lang/rust#141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
@xizheyin xizheyin deleted the issue-141138 branch June 1, 2025 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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
6 participants