docs

#E0073: impl 項目不在 trait 中

trait 的 impl 定義了 trait 沒有宣告的方法或關聯型別。刪除它,或把方法移到該型別的固有 impl。trait 的泛型參數不是關聯型別:要寫在標頭中,例如 impl IndexSet<usize, V> for T,而不是寫成 type V = ..。

nabi
trait Shape { fn area(self) -> f64; }
struct Sq { s: f64; }
impl Shape for Sq {
    fn area(self) -> f64 { self.s * self.s }
    fn side(self) -> f64 { self.s }
}
error[E0073]: method `side` is not a member of trait `Shape`
  --> main.nabi:3:1
   |
 3 | impl Shape for Sq {
   | ^^^^

把方法移到固有 impl:

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

相關錯誤碼:E0072 impl 缺少 trait 項目、E0074 impl 項目與 trait 不符

發現錯誤? 在 GitHub 上編輯此頁 最後更新: