[rust-dev] Cloning a statically-sized array

Daniel Micay danielmicay at gmail.com
Sat Jan 18 21:19:46 PST 2014


On Sun, Jan 19, 2014 at 12:17 AM, Ashish Myles <marcianx at gmail.com> wrote:
> Now, I already know that statically-sized arrays of primitives are
> implicitly copyable, but consider, for example, a statically-sized array of
> a non-copyable but Clone-able type.  I find that for v of type [T, ..2],
> v.clone() is not a static array.  Perhaps it's because v is being implicitly
> treated as &[T] instead.
>
> Eg.
> --------------------------------------------------
> fn make_clone<T : Clone>(a : &T) -> T {
>     a.clone();
> }
>
> fn main() {
>     let a : [int, ..2] = [1, 2];
>     // error: failed to find an implementation of trait std::clone::Clone
>     // for [int, .. 2]
>     make_clone(&a);
>
>     let a : [int, ..2] = [1, 2];
>     // error: mismatched types: expected `[int, .. 2]` but found `&[int]`
>     // ([] storage differs: expected 2 but found &)
>     let b : [int, ..2] = a.clone();
> }
> --------------------------------------------------
>
> So is it a missing feature of rust that Clone is not supported generated for
> statically-sized arrays or is there a more fundamental reason that it
> doesn't exist?
>
> Ashish

Clone is entirely a library feature, and Rust currently provides no
way to implement methods on fixed-size arrays. The latter is the real
issue, because there are other methods like `Eq` that should be
implemented on them too.


More information about the Rust-dev mailing list