#E0076: 為外部型別實作外部 trait
trait impl 要求 trait 或型別至少有一個定義在這個套件中。把外部型別包在你自己的結構中,再為該結構實作 trait。
nabi
impl Display for List<i32> {
fn fmt(self, ref f: Formatter) throws (FmtError) { try f.write_str("list"); }
}
error[E0076]: `impl Display for List<i32>`: neither the trait nor the type is defined in this package (orphan rule) --> main.nabi:1:1 | 1 | impl Display for List<i32> { | ^^^^
把外部型別包在你自己的結構中:
nabi
struct Nums { xs: List<i32>; }
impl Display for Nums {
fn fmt(self, ref f: Formatter) throws (FmtError) { try f.write_str(f"{self.xs.len()} numbers"); }
}
println(f"{Nums { xs: [1, 2] }}");
2 numbers
相關錯誤碼:E0075 互相衝突的實作
發現錯誤? 在 GitHub 上編輯此頁 最後更新: