docs

v0.1.2 문서를 보고 있습니다. 이 버전은 더 이상 지원하지 않습니다. 최신 릴리스: v0.1.3 →

#E0075: 충돌하는 구현

두 impl이 한 타입에 같은 트레잇이나 같은 메서드를 줍니다. 하나를 지우거나, 제네릭 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 { .. }`

트레잇과 타입마다 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 외부 타입에 대한 외부 트레잇의 impl, E0087 모호한 연관 항목

잘못된 곳이 있나요? GitHub에서 이 페이지 고치기. 마지막 수정