A fixed-length array of a native type (or of a type that implements the Copy
trait) can be cloned in Rust up to the length of 32. That is, this compiles:
You can clone arbitrary-length arrays since Rust 1.21.0. The "Libraries" section of the CHANGELOG says:
Generate builtin impls for
Clone
for all arrays and tuples that areT: Clone
You can't add the impl Clone
in your own code. This problem will be fixed at some point, in the mean time you can mostly work around it with varying amount of effort:
Copy
(as in your example), you can simply copy rather than cloning, i.e., let _cloned = source;
.Clone
for (and derive
won't work), you can still manually implement Clone
and using the above trick in the implementation.Copy
types is trickier, because Clone
can fail. You could write out [x[0].clone(), x[1].clone(), ...]
for as many times as you need, it's a lot of work but at least it's certain to be correct.Clone
.