如何克隆长度大于32的数组?

时间:2021-08-08 21:45:22

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:

固定长度的本机类型数组(或实现复制特征的类型)可以在Rust中克隆到32的长度。也就是说,这会编译:

fn main() {
    let source: [i32; 32] = [0; 32]; // length 32
    let _cloned = source.clone();
}

But this doesn't:

但这不是:

fn main() {
    let source: [i32; 33] = [0; 33]; // length 33
    let _cloned = source.clone(); // <-- compile error
}

In fact, the trait Clone only declares a method for each generic array length, from 0 to 32.

实际上,特性克隆只为每个通用数组长度声明一个方法,从0到32。

What is an efficient and idiomatic way to clone a generic array of length, say, 33?

什么是克隆长度的通用数组(例如33)的有效且惯用的方法?

1 个解决方案

#1


7  

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:

您无法在自己的代码中添加impl Clone。这个问题将在某个时候得到修复,同时您可以通过不同的努力来解决这个问题:

  • If you just have a local variable of a concrete type and the type is Copy (as in your example), you can simply copy rather than cloning, i.e., let _cloned = source;.
  • 如果您只有一个具体类型的局部变量且类型为Copy(如您的示例所示),则可以简单地复制而不是克隆,即让_cloned = source;。
  • If the array is a field of a struct you want to implement Clone for (and derive won't work), you can still manually implement Clone and using the above trick in the implementation.
  • 如果数组是想要实现Clone for的结构的字段(并且派生将不起作用),您仍然可以手动实现Clone并在实现中使用上述技巧。
  • Cloning an array of non-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.
  • 克隆非复制类型的数组比较棘手,因为克隆可能会失败。您可以根据需要多次写出[x [0] .clone(),x [1] .clone(),...],这是很多工作,但至少它确定是正确的。
  • If all else fails, you can still create a newtype wrapper. This requires quite a bit of boilerplate to delegate all the other traits you need, but then you can (again, manually) implement Clone.
  • 如果所有其他方法都失败了,您仍然可以创建一个newtype包装器。这需要相当多的样板来委托您需要的所有其他特征,但随后您可以(再次,手动)实现克隆。

#1


7  

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:

您无法在自己的代码中添加impl Clone。这个问题将在某个时候得到修复,同时您可以通过不同的努力来解决这个问题:

  • If you just have a local variable of a concrete type and the type is Copy (as in your example), you can simply copy rather than cloning, i.e., let _cloned = source;.
  • 如果您只有一个具体类型的局部变量且类型为Copy(如您的示例所示),则可以简单地复制而不是克隆,即让_cloned = source;。
  • If the array is a field of a struct you want to implement Clone for (and derive won't work), you can still manually implement Clone and using the above trick in the implementation.
  • 如果数组是想要实现Clone for的结构的字段(并且派生将不起作用),您仍然可以手动实现Clone并在实现中使用上述技巧。
  • Cloning an array of non-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.
  • 克隆非复制类型的数组比较棘手,因为克隆可能会失败。您可以根据需要多次写出[x [0] .clone(),x [1] .clone(),...],这是很多工作,但至少它确定是正确的。
  • If all else fails, you can still create a newtype wrapper. This requires quite a bit of boilerplate to delegate all the other traits you need, but then you can (again, manually) implement Clone.
  • 如果所有其他方法都失败了,您仍然可以创建一个newtype包装器。这需要相当多的样板来委托您需要的所有其他特征,但随后您可以(再次,手动)实现克隆。