Elaborate on deriving vs implementing Copy

This commit is contained in:
Godfrey Chan 2024-09-03 11:27:34 -07:00 committed by GitHub
parent d6c8169c18
commit 65e78db8d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -288,8 +288,18 @@ pub trait StructuralPartialEq {
/// } /// }
/// ``` /// ```
/// ///
/// There is a small difference between the two: the `derive` strategy will also place a `Copy` /// There is a small difference between the two. The `derive` strategy will also place a `Copy`
/// bound on type parameters, which isn't always desired. /// bound on type parameters:
///
/// ```
/// struct MyStruct<T>;
///
/// impl<T: Copy> Copy for MyStruct<T> { }
/// ```
///
/// This isn't always desired. For example, shared references (`&T`) can be copied regardless of
/// whether `T` is `Copy`. Likewise, a generic struct containing markers such as [`PhantomData`]
/// could potentially be duplicated with a bit-wise copy.
/// ///
/// ## What's the difference between `Copy` and `Clone`? /// ## What's the difference between `Copy` and `Clone`?
/// ///