Question about specialization behaviour with Clone and Copy

Hey folks,

I was reading in the forum and found this topic which in turn led me to this topic where @steffahn posted this code snippet.

You can opt-in or opt-out the clone method of Clone via implementing Copy. I understand what this is good for but I don't understand why Rust "behaves" this way. Could anybody explain why this works this way? Is this behaviour documented anywhere?

Regards
keks

1 Like

This is hinted at in docs for Clone.

It's implemented using specialization, which is an unstable/experimental feature that you can't normally use in stable Rust.

Also Clone is a "lang item", which means it has special-case implementations in the compiler.

In library/core/src/slice/mod.rs

3 Likes

I see! :slight_smile: Thanks a lot!

See 1521-copy-clone-semantics - The Rust RFC Book for some background.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.