#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 上編輯此頁 最後更新: