E0905: Array repeat non copy
| Code | Category | Stability |
|---|---|---|
E0905 | Array | Permanent |
Explanation
The repeat form [value; count] materializes count copies of a single value: the value expression is evaluated exactly once and its result is copied into each slot (7.1:39). Copying is only well-defined when the element type is Copy, so a repeat literal requires one (7.1:38); E0905 names the element type that is not. Rue's Copy types are the integer types, bool, the unit type, the first-class string types, discriminant-only enums, and arrays of Copy elements (3.8:2); a user-defined struct is a move type by default however small its fields are (3.8:3), and opts in with the @copy directive (3.8:14). The list form [a, b, c] carries no such requirement — each element is a separate value-context use, so non-Copy elements can be moved into it one at a time.
Likely cause
The repeated value's type is a struct that is not marked @copy, or an aggregate holding such a struct. Mark the struct @copy when every field is itself Copy and the type has no destructor, or write the list form and move a distinct value into each position. An element type with a destructor or a must-consume obligation can never be Copy, so those must use the list form.
Examples
Repeat a move-type element
struct Data { value: i32 }
fn main() -> i32 {
let d = Data { value: 1 };
let xs = [d; 3];
0
}
Declare the element type Copy
@copy
struct Data { value: i32 }
fn main() -> i32 {
let d = Data { value: 1 };
let xs = [d; 3];
xs[0].value + xs[1].value + xs[2].value
}