#E0069: 不满足 trait 约束
某个类型没有实现泛型参数、关联类型或位置所要求的 trait。为该类型实现这个 trait,或者使用实现了它的类型。重复字面量 [v; N] 把 v 克隆到每个元素中,所以 v 必须是 Clone,并且不能是只有一个所有者的资源;这样的数组用 array::from_fn::<T, N>(|_| ..) 逐个元素构造。
nabi
fn keep<T: Clone>(x: T) -> T { x }
struct P { x: i32; }
println(f"{keep(P { x: 1 }).x}");
error[E0069]: type `P` does not satisfy trait bound `Clone` required by `keep` --> main.nabi:4:12 | 4 | println(f"{keep(P { x: 1 }).x}"); | ^^^^^^^^^^^^^^^^
为该类型实现这个 trait,这里通过派生实现:
nabi
fn keep<T: Clone>(x: T) -> T { x }
#[derive(Clone)]
struct P { x: i32; }
println(f"{keep(P { x: 1 }).x}");
1
重复字面量 [v; N] 用 clone() 给每个元素一份 v 的副本,所以含有集合的元素类型也必须是 Clone:
nabi
struct Bag { xs: List<i32>; }
let bags = [Bag { xs: [] }; 3];
error[E0069]: `[v; N]` copies `v` into each element with `clone()`, but `Bag` is not `Clone` --> main.nabi:3:12 | 3 | let bags = [Bag { xs: [] }; 3]; | ^^^^^^^^^^^^^^^^^^^ | = help: build each element on its own: `array::from_fn::<Bag, 3>(|_| ..)`
为该类型派生 Clone,或者逐个构造每个元素:
nabi
struct Bag { xs: List<i32>; }
let bags = array::from_fn::<Bag, 3>(|_| Bag { xs: [] });
bags[0].xs.push(1);
println(f"{bags.len()} {bags[0].xs:?} {bags[1].xs:?}");
3 [1] []
相关错误码:E0054 无法应用运算符,E0077 未实现父 trait,E0088 无效的格式
发现错误? 在 GitHub 上编辑此页 最后更新: