Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Why is Copy a subtrait of Clone? Understanding the design rationale behind semantic inconsistency
Hi everyone,
I've been thinking about the Copy/Clone trait relationship and struggling to understand the design rationale. I've searched extensively but couldn't find discussions about this specific design decision, so I'm hoping someone here can help clarify or point me to relevant resources.
Currently, Copy is a subtrait of Clone, which allows this potentially confusing behavior:
#[derive(Copy, Debug)]
struct Example(u32);
impl Clone for Example {
fn clone(&self) -> Example {
Example(1) // Different behavior than assignment!
}
}
fn main() {
let a = Example(2);
let b = a; // Copy semantics: Example(2)
let c = a.clone(); // Custom Clone: Example(1)
println!("{:?} {:?} {:?}", a, b, c); // Example(2) Example(2) Example(1)
}
According to RFC 1521, Copy types should guarantee that Clone::clone == ptr::read for T: Copy
. However, the current design allows custom Clone implementations that can violate this invariant, leading to potentially confusing behavior where assignment and .clone()
do different things.
I keep wondering why Rust didn't adopt this alternative approach:
impl<T: Copy> Clone for T
in libcore for all types that implements Copy to have a ptr::read
based clone()
implemention.ptr::read
T: Copy
for "cheap implicit copy that always ptr::read
" vs T: Clone
for "potentially expensive explicit copy".clone()
behavior mismatch#[derive(Copy, Clone)]
- just #[derive(Copy)]
This seems technically feasible:
What am I missing? The alternative design seems to offer better semantics, simpler implementation, and improved developer experience. Are there technical constraints or design considerations I'm overlooking?
Where can I find discussions about such design decisions? I'd love to read the original discussions that led to the current Copy/Clone relationship, but I'm not sure where to look for historical design rationale.
I'm genuinely curious to understand the reasoning behind this design choice. Any insights or pointers to relevant discussions would be greatly appreciated!
Thanks for your patience with my questions!
7 posts - 3 participants
🏷️ Rust_feed