-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
/// | ||
/// 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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to change it here, also similar to Trait Clone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, I think the summary list actually fits better here. Thanks! |
||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -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. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.