当我尝试用 const 初始化字符串数组时,一切都很好:
fn main() {
const ARRAY_REPEAT_VALUE: String = String::new();
let mut strings: [String; 5] = [ARRAY_REPEAT_VALUE; 5];
}
但是当我用不可变变量替换 const 时,出现错误:
fn main() {
let array_repeat_value: String = String::new();
let mut strings: [String; 5] = [array_repeat_value; 5];
}
错误是:
error[E0277]: the trait bound `String: Copy` is not satisfied
--> src/main.rs:3:37
|
3 | let mut strings: [String; 5] = [array_repeat_value; 5];
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
|
= note: the `Copy` trait is required because this value will be copied for each element of the array
即使我使用 const,我也期望得到复制特征错误。如何解释这种行为?