diff options
| author | gewitternacht <60887951+gewitternacht@users.noreply.github.com> | 2025-07-22 23:16:49 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 23:16:49 +0200 |
| commit | 30033b45bb2fcbc0f267acaf9fab22411422e166 (patch) | |
| tree | f3893c2670582edb68baedba070117df43d0811a /library/core/src/clone.rs | |
| parent | e05ab47e6c418fb2b9faa2eae9a7e70c65c98eaa (diff) | |
| download | rust-30033b45bb2fcbc0f267acaf9fab22411422e166.tar.gz rust-30033b45bb2fcbc0f267acaf9fab22411422e166.zip | |
document assumptions about `Clone` and `Eq` traits
Diffstat (limited to 'library/core/src/clone.rs')
| -rw-r--r-- | library/core/src/clone.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index a34d1b4a064..7afdf50ce51 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -139,6 +139,30 @@ mod uninit; /// // Note: With the manual implementations the above line will compile. /// ``` /// +/// ## `Clone` and `PartialEq`/`Eq` +/// `Clone` is intended for the duplication of objects. Consequently, when implementing +/// both `Clone` and [`PartialEq`], the following property is expected to hold: +/// ```text +/// x == x -> x.clone() == x +/// ``` +/// In other words, if an object compares equal to itself, +/// its clone must also compare equal to the original. +/// +/// For types that also implement [`Eq`] – for which `x == x` always holds – +/// this implies that `x.clone() == x` must always be true. +/// Standard library collections such as +/// [`HashMap`], [`HashSet`], [`BTreeMap`], [`BTreeSet`] and [`BinaryHeap`] +/// rely on their keys respecting this property for correct behavior. +/// +/// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`] +/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] +/// using `#[derive(Clone, PartialEq, Eq)]`. +/// +/// Violating this property is a logic error. The behavior resulting from a logic error is not +/// specified, but users of the trait must ensure that such logic errors do *not* result in +/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these +/// methods. +/// /// ## Additional implementors /// /// In addition to the [implementors listed below][impls], |
