本頁尚未翻譯,以下為英文原文。
#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