docs

#E0074: impl 项与 trait 不一致

trait impl 中的方法在参数、返回类型或 throws 上与 trait 的声明不同。按 trait 的声明书写它。

nabi
trait Shape { fn area(self) -> f64; }
struct Sq { s: f64; }
impl Shape for Sq {
    fn area(self, scale: f64) -> f64 { self.s * scale }
}
error[E0074]: `area` does not match its declaration in trait `Shape`: it takes 2 parameters where the trait's takes 1
  --> main.nabi:4:5
   |
 4 |     fn area(self, scale: f64) -> f64 { self.s * scale }
   |     ^^
   |
   = help: write `fn area(self) -> f64`

按 trait 的声明书写方法:

nabi
trait Shape { fn area(self) -> f64; }
struct Sq { s: f64; }
impl Shape for Sq {
    fn area(self) -> f64 { self.s * self.s }
}
println(f"{Sq { s: 2.0 }.area()}");
4

相关错误码:E0072 impl 缺少 trait 项,E0073 impl 项不在 trait 中

发现错误? 在 GitHub 上编辑此页 最后更新: