[rust-dev] Cloning a statically-sized array

Ashish Myles marcianx at gmail.com
Sat Jan 18 21:17:57 PST 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140119/71adcce5/attachment.html>


More information about the Rust-dev mailing list