#E0072: impl에 빠진 트레잇 항목
트레잇 impl은 트레잇이 기본 구현 없이 선언한 메서드, 연관 타입, 연관 상수를 모두 정의해야 합니다. 빠진 항목을 추가하세요(type Item = i32;).
nabi
trait Shape {
fn area(self) -> f64;
fn name(self) -> str;
}
struct Sq { s: f64; }
impl Shape for Sq {
fn area(self) -> f64 { self.s * self.s }
}
error[E0072]: impl of trait `Shape` for `Sq` is missing method `name` --> main.nabi:6:1 | 6 | impl Shape for Sq { | ^^^^
빠진 메서드를 추가하세요.
nabi
trait Shape {
fn area(self) -> f64;
fn name(self) -> str;
}
struct Sq { s: f64; }
impl Shape for Sq {
fn area(self) -> f64 { self.s * self.s }
fn name(self) -> str { "square" }
}
println(Sq { s: 1.0 }.name());
square
트레잇이 기본값 없이 선언한 연관 타입도 정해야 합니다. 도움말은 impl의 메서드가 그 자리에 쓴 타입을 알려 줍니다.
nabi
struct Count { n: i32; }
impl Iterator for Count {
fn next(ref self) -> i32 | none {
if self.n > 2 { return none; }
self.n += 1;
self.n
}
}
error[E0072]: impl of `Iterator` for `Count` is missing associated type `Item` --> main.nabi:2:1 | 2 | impl Iterator for Count { | ^^^^ | = help: the trait declares `type Item` with no default: add `type Item = i32;` to the impl
연관 타입을 추가하세요.
nabi
struct Count { n: i32; }
impl Iterator for Count {
type Item = i32;
fn next(ref self) -> i32 | none {
if self.n > 2 { return none; }
self.n += 1;
self.n
}
}
for v in (Count { n: 0 }) { println(f"{v}"); }
1
2
3
관련 코드: E0073 트레잇에 없는 impl 항목, E0074 트레잇과 맞지 않는 impl 항목
잘못된 곳이 있나요? GitHub에서 이 페이지 고치기. 마지막 수정