docs

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

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

#E0072: impl is missing a trait item

A trait impl must define every method, associated type and associated constant the trait declares without a default. Add the missing items (type Item = i32;).

nabi
trait Shape {
    fn area(self) -> f64;
    fn name(self) -> str;
}
struct Sq { s: f64; }
impl Shape for Sq {
    fn area(self) -> f64 { self.s * self.s }
}
error[E0072]: impl of trait `Shape` for `Sq` is missing method `name`
  --> main.nabi:6:1
   |
 6 | impl Shape for Sq {
   | ^^^^

Add the missing method:

nabi
trait Shape {
    fn area(self) -> f64;
    fn name(self) -> str;
}
struct Sq { s: f64; }
impl Shape for Sq {
    fn area(self) -> f64 { self.s * self.s }
    fn name(self) -> str { "square" }
}
println(Sq { s: 1.0 }.name());
square

An associated type the trait declares without a default must be given too; the help names the type the impl's methods use for it:

nabi
struct Count { n: i32; }
impl Iterator for Count {
    fn next(ref self) -> i32 | none {
        if self.n > 2 { return none; }
        self.n += 1;
        self.n
    }
}
error[E0072]: impl of `Iterator` for `Count` is missing associated type `Item`
  --> main.nabi:2:1
   |
 2 | impl Iterator for Count {
   | ^^^^
   |
   = help: the trait declares `type Item` with no default: add `type Item = i32;` to the impl

Add the associated type:

nabi
struct Count { n: i32; }
impl Iterator for Count {
    type Item = i32;
    fn next(ref self) -> i32 | none {
        if self.n > 2 { return none; }
        self.n += 1;
        self.n
    }
}
for v in (Count { n: 0 }) { println(f"{v}"); }
1
2
3

Related: E0073 impl item is not in the trait, E0074 impl item does not match the trait

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