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 上編輯此頁 最後更新: