# Array constructor for Clone types

**URL:** <https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748>\
**Category:** language design\
**Created:** [February 4, 2020, 8:32pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748 "2020-02-04T20:32:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 4, 2020, 8:32pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/1 "2020-02-04T20:32:17Z")

</div>

I have run across this problem where I want to make an array (fixed size) using a constructor where the type implements `Clone` but not `Copy`.

I know that it is possible to use `Default` (if that exists for the type) and then overwrite each index but that can be either messy at best or impossible at worst.

So it there any reason why the following shouldn't exist? (assuming `T` is actually a concrete type)

```rust
fn array_6<T: Clone>() -> [T; 6] {
    [T::new(); 6]
}

```

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [February 4, 2020, 8:41pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/2 "2020-02-04T20:41:39Z")

</div>

I'm not understanding the question. `T::new()` is not a method on the `Clone` trait. The concrete type behind `T` may not have a `new()` method. It's not possible to magically synthesize a `new()` method for an arbitrary type, since the compiler has no idea what invariants might apply to the type's internal state. You can't build `new()` out of `clone()` since `clone()` requires a value of `T` to already exist.

So, how could this work without an additional trait bound like `Default`?

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 4, 2020, 8:59pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/3 "2020-02-04T20:59:40Z")

</div>

Sorry, that is my fault. I didn't explain this very well.

The following fails:

```rust
fn foo() -> [Vec<i32>; 6] {
    [vec![1, 2]; 6]
}

```

With:

```rust
the trait `std::marker::Copy` is not implemented for `std::vec::Vec<i32>`

= note: the `Copy` trait is required because the repeated element will be copied

```

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [February 4, 2020, 9:35pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/4 "2020-02-04T21:35:39Z")

</div>

Ah, _that_ makes sense.

I don't have any citations for this, but my impression is that potentially expensive copies (i.e., excluding `Clone` impls for `Copy` types) are intentionally never permitted without some explicit syntactic marker at the clone site. Which I generally agree with.

But I do think it would be silly to demand N separate syntactic markers whenever N clones happen at "the same place" like this. I don't see anything like this in `std` already, and I haven't heard of a crate for this, so here's one strawman idea that I would endorse:

```rust
[Vec<i32>; 6]::from_clones_of(vec![1,2]);

```

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 4, 2020, 9:50pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/5 "2020-02-04T21:50:36Z")

</div>

That would require const generics which probably is fine.

But I disagree in a place like this that implicitly cloning is bad.

---

<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:** [February 4, 2020, 10:06pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/6 "2020-02-04T22:06:59Z")

</div>

The similar `vec![expr; N]` does use `Clone`. I guess you can attribute this design difference to the different teams involved, being a language or library feature.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [February 4, 2020, 10:34pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/7 "2020-02-04T22:34:28Z")

</div>

URLO had a thread about the same thing recently:

> **[Array of non-Copy](https://users.rust-lang.org/t/array-of-non-copy/37682/3?u=scottmcm)**
>
> The situation here will probably get better as const generics get stabilized. In the mean time there are a bunch of crates that can help. To add to the one already mentioned, https://crates.io/crates/arraytools allows things like \<\[\_;...

---

<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:** [February 4, 2020, 10:38pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/8 "2020-02-04T22:38:27Z")

</div>

I guess it's the partial drop on panics that make this harder for arrays, whereas `vec!` naturally knows the length written so far.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [February 4, 2020, 11:22pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/9 "2020-02-04T23:22:13Z")

</div>

Almost every question "why don't arrays just…?" is because arrays in Rust are half-baked. There's no profound reason. They just can't be improved much until Rust finalizes the const generics feature.

In the meantime you can use wrappers on arrays that make them a little bit more usable, e.g.:

> **[arrayvec](https://lib.rs/crates/arrayvec)**
>
> A vector with fixed capacity, backed by an array (it can be stored on the stack too). Implements fixed capacity ArrayVec and ArrayString.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 4, 2020, 11:33pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/10 "2020-02-04T23:33:57Z")

</div>

But the following works:

```rust
let arr = [
    vec![1, 2],
    vec![1, 2],
    vec![1, 2],
    vec![1, 2],
    vec![1, 2],
    vec![1, 2],
];

```

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [February 4, 2020, 11:45pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/11 "2020-02-04T23:45:17Z")

</div>

Note that it isn't equivalent to one that uses clone; that'd be

```
let tmp = vec![1, 2];
let _ = [
    tmp.clone(),
    tmp.clone(),
    tmp.clone(),
    tmp.clone(),
    tmp.clone(),
    tmp,
];

```

This will be a trivial helper once we have const generics; in the mean time, you could potentially make a macro for it if you can accept using weird number representations.

---

<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:** [May 4, 2020, 11:45pm UTC](https://internals.rust-lang.org/t/array-constructor-for-clone-types/11748/12 "2020-05-04T23:45:24Z")

</div>

Closed on May 4, 2020, 11:45pm UTC

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