# Incorrect \`Box::clone\` doc

**URL:** <https://internals.rust-lang.org/t/incorrect-box-clone-doc/18960>\
**Category:** documentation\
**Created:** [June 9, 2023, 9:46am UTC](https://internals.rust-lang.org/t/incorrect-box-clone-doc/18960 "2023-06-09T09:46:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)\
**Post date:** [June 9, 2023, 9:46am UTC](https://internals.rust-lang.org/t/incorrect-box-clone-doc/18960/1 "2023-06-09T09:46:57Z")

</div>

The current document is

> <https://github.com/rust-lang/rust/blob/9c843d9fa322596c7d525c78fa89731ecf7afbfe/library/alloc/src/boxed.rs#LL1256C1-L1269C12>

> a `clone()` of this box's contents.

But in reality, its behavior is specialized, if the content impl `Copy`, will not call `clone`.

* * *

What is our expected behavior, should we modify the document or remove specialization?

---

<div class="post-metadata">

**Author:** ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)\
**Post date:** [June 9, 2023, 10:46am UTC](https://internals.rust-lang.org/t/incorrect-box-clone-doc/18960/2 "2023-06-09T10:46:33Z")

</div>

[Incorrect `Box::clone` doc · Issue #112455 · rust-lang/rust (github.com)](https://github.com/rust-lang/rust/issues/112455)

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [June 9, 2023, 5:23pm UTC](https://internals.rust-lang.org/t/incorrect-box-clone-doc/18960/5 "2023-06-09T17:23:18Z")

</div>

This is relying on the semantics of [RFC 1521](https://rust-lang.github.io/rfcs/1521-copy-clone-semantics.html), which led to [this note](https://doc.rust-lang.org/std/clone/trait.Clone.html#how-can-i-implement-clone) in the `Clone` docs:

> Types that are [`Copy`](https://doc.rust-lang.org/std/marker/trait.Copy.html) should have a trivial implementation of `Clone`. More formally: if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`. Manual implementations should be careful to uphold this invariant; however, unsafe code must not rely on it to ensure memory safety.

> [@Qsh](#):
>
> if the content impl `Copy`, will not call `clone`.

So if you're doing anything in `clone` that is _observably_ different than a `Copy`, then you are violating this expectation. That shouldn't cause any safety problems, as noted above, but you could see that `dbg!` statements in your `clone` aren't called if it was actually copied instead.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 7, 2023, 5:23pm UTC](https://internals.rust-lang.org/t/incorrect-box-clone-doc/18960/6 "2023-09-07T17:23:29Z")

</div>

Closed on September 7, 2023, 5:23pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
