docs

#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 上编辑此页 最后更新: