docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

本頁尚未翻譯,以下為英文原文。

#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>(|_| ..).

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}");
   |            ^^^^^^^^^^^^^^^^

Implement the trait for the type, here by deriving it:

nabi
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:

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>(|_| ..)`

Derive Clone for the type, or build each element on its own:

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] []

Related: E0054 operator cannot be applied, E0077 supertrait not implemented, E0088 invalid format

Found a mistake? Edit this page on GitHub. Last updated