docs

你正在阅读 v0.1.0 的文档。Nabi 0.1.0 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#E0076: impl of a foreign trait for a foreign type

A trait impl needs the trait or the type to be defined in this package. Wrap the foreign type in a struct of your own and implement the trait for that.

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> {
   | ^^^^

Wrap the foreign type in a struct of your own:

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

Related: E0075 conflicting implementations

Found a mistake? Edit this page on GitHub. Last updated