# Copy and clone a custom struct

**URL:** <https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782>\
**Category:** help\
**Created:** [November 22, 2020, 1:17am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782 "2020-11-22T01:17:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mor](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mor](https://users.rust-lang.org/u/mor)\
**Post date:** [November 22, 2020, 1:17am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/1 "2020-11-22T01:17:07Z")

</div>

Hi, I am trying to create a copy implementation to a structure with Array2D and a simple array. The compiler doesn't like my implementation. Thanks for any help. Mor

```rust
struct Cube1
{
    pub s1: Array2D<i32>,
    pub s2: Array2D<i32>,
    pub s3: Array2D<i32>,
    pub s4: Array2D<i32>,
    pub s5: Array2D<i32>,
    pub s6: Array2D<i32>,
    pub s1_sides: [i32; 4],
    pub s2_sides: [i32; 4],
    pub s3_sides: [i32; 4],
    pub s4_sides: [i32; 4],
    pub s5_sides: [i32; 4],
    pub s6_sides: [i32; 4],
}

impl Copy for Cube1
{
    fn copy(&self) -> Cube1 {
        return Cube1 {
            s1: Array2D::from_rows(self.s1.as_rows()),
            s2: Array2D::from_rows(self.s2.as_rows()),
            s3: Array2D::from_rows(self.s3.as_rows()),
            s4: Array2D::from_rows(self.s4.as_rows()),
            s5: Array2D::from_rows(self.s5.as_rows()),
            s6: Array2D::from_rows(self.s6.as_rows()),
            s1_sides: self.s1_sides,
            s2_sides: self.s2_sides,
            s3_sides: self.s3_sides,
            s4_sides: self.s4_sides,
            s5_sides: self.s5_sides,
            s6_sides: self.s6_sides,
        }
    }
}

```

The error I am getting:

```rust
   |
31 | / fn copy(&self) -> Cube1 {
32 | | return Cube1 {
33 | | s1: Array2D::from_rows(self.s1.as_rows()),
34 | | s2: Array2D::from_rows(self.s2.as_rows()),
... |
45 | | }
46 | | }
   | | _____ ^ not a member of trait `Copy`

error[E0204]: the trait `Copy` may not be implemented for this type
  --> src\cube.rs:29:6
   |
15 | pub s1: Array2D<i32>,
   | -------------------- this field does not implement `Copy`
16 | pub s2: Array2D<i32>,
   | -------------------- this field does not implement `Copy`
17 | pub s3: Array2D<i32>,
   | -------------------- this field does not implement `Copy`
18 | pub s4: Array2D<i32>,
   | -------------------- this field does not implement `Copy`
19 | pub s5: Array2D<i32>,
   | -------------------- this field does not implement `Copy`
20 | pub s6: Array2D<i32>,
   | -------------------- this field does not implement `Copy`
...
29 | impl Copy for Cube1
   | ^^^^

```

---

<div class="post-metadata">

**Author:** ![Yandros](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yandros/32/8136_2.png) [@Yandros](https://users.rust-lang.org/u/Yandros)\
**Post date:** [November 22, 2020, 1:29am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/2 "2020-11-22T01:29:54Z")

</div>

The [`Copy`](https://doc.rust-lang.org/stable/core/marker/trait.Copy.html) trait has no methods: it is a marker trait used by the compiler not to invalidate "old values" on "move", which are implemented as _shallow bit-wise copies_.

The [`Clone`](https://doc.rust-lang.org/stable/core/clone/trait.Clone.html) trait is the one where you can override the "copying" behavior, but only do that if you are not implementing `Copy` already (since when something is `Copy` (and thus `Clone`), it is expected that `Clone` simply delegate to `Copy`).

- The error message:

---

<div class="post-metadata">

**Author:** ![mor](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mor](https://users.rust-lang.org/u/mor)\
**Post date:** [November 22, 2020, 1:35am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/3 "2020-11-22T01:35:40Z")

</div>

The clone worked but that mean that I have to do: let d = c.clone();  
instead of doing: let d = c;

---

<div class="post-metadata">

**Author:** ![TomP](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tomp/32/4735_2.png) [@TomP](https://users.rust-lang.org/u/TomP)\
**Post date:** [November 22, 2020, 1:37am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/4 "2020-11-22T01:37:21Z")

</div>

But if `c` can't be copied, because it contains fields that can't be copied, why would you expect that

> [@mor](#):
>
> let d = c

would somehow _magically_ copy it?

---

<div class="post-metadata">

**Author:** ![mor](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mor](https://users.rust-lang.org/u/mor)\
**Post date:** [November 22, 2020, 1:38am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/5 "2020-11-22T01:38:08Z")

</div>

I thought that I can implement a copy function like I did for the clone.

---

<div class="post-metadata">

**Author:** ![mor](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mor](https://users.rust-lang.org/u/mor)\
**Post date:** [November 22, 2020, 1:39am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/6 "2020-11-22T01:39:07Z")

</div>

Thanks for the help

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [November 22, 2020, 5:59am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/7 "2020-11-22T05:59:44Z")

</div>

In addition to `Copy` meaning "a shallow bit for bit copy" as @Yandros said, you can think of this restriction as "no implicit non-`Copy` clones". Implicit clones would interact poorly with the ownership model.

```rust
// Hopefully I don't .clone() because I'm just trying to move ownership!
struct.frob = complicated_thing;

```

(And Rust tends to prefer potentially expensive operations be explicit to boot.)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 20, 2021, 5:59am UTC](https://users.rust-lang.org/t/copy-and-clone-a-custom-struct/51782/8 "2021-02-20T05:59:46Z")

</div>

Closed on February 20, 2021, 5:59am UTC

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.
