docs

#E0075: 相互冲突的实现

两个 impl 为同一个类型提供了同一个 trait 或同一个方法。删除其中一个,或者收窄泛型 impl,使它们不再重叠。

nabi
trait Show { fn show(self) -> str; }
impl<T> Show for T { fn show(self) -> str { "any" } }
impl Show for i32 { fn show(self) -> str { "i32" } }
error[E0075]: conflicting implementations of trait `Show` for `i32`: the concrete impl overlaps `impl<..> Show for T` (one impl per trait and type; no specialization)
  --> main.nabi:2:18
   |
 2 | impl<T> Show for T { fn show(self) -> str { "any" } }
   |                  ^
   |
   = help: put the `i32` case in the generic impl: `if Self is i32 { .. }`

每个 trait 和类型只保留一个 impl:

nabi
trait Show { fn show(self) -> str; }
impl Show for i32 { fn show(self) -> str { "i32" } }
println(3.show());
i32

或者保留泛型 impl,在其中给 i32 一个单独的分支:

nabi
trait Show { fn show(self) -> str; }
impl<T> Show for T {
    fn show(self) -> str {
        if Self is i32 { "i32" } else { "any" }
    }
}
println(f"{3i32.show()} {1.5.show()}");
i32 any

相关错误码:E0076 为外部类型实现外部 trait,E0087 有歧义的关联项

发现错误? 在 GitHub 上编辑此页 最后更新: