本页尚未翻译,以下为英文原文。
#E0069: trait bound not satisfied
A type does not implement a trait that a generic parameter, an associated type or a slot requires. Implement the trait for the type, or use a type that has it. A repeat literal [v; N] clones v into each element, so v must be Clone and not a resource with one owner; build such an array element by element with array::from_fn::<T, N>(|_| ..).
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}"); | ^^^^^^^^^^^^^^^^
Implement the trait for the type, here by deriving it:
fn keep<T: Clone>(x: T) -> T { x }
#[derive(Clone)]
struct P { x: i32; }
println(f"{keep(P { x: 1 }).x}");
1
A repeat literal [v; N] gives each element its own copy of v with clone(), so an element type that holds a collection must be Clone too:
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>(|_| ..)`
Derive Clone for the type, or build each element on its own:
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] []
Related: E0054 operator cannot be applied, E0077 supertrait not implemented, E0088 invalid format
Found a mistake? Edit this page on GitHub. Last updated