在实现Draw
trait的类型定义中,必须指定item
的具体类型。
例如,如果我们有一个带有关联类型item
的Draw
trait:
trait Draw {
type item;
fn draw(&self, item: &Self::item);
}
我们可以为item
指定具体类型,如下所示:
struct Rectangle {
width: u32,
height: u32,
}
impl Draw for Rectangle {
type item = Rectangle;
fn draw(&self, item: &Self::item) {
println!("Drawing rectangle ({}, {}) with width {} and height {}",
item.width, item.height, self.width, self.height);
}
}
上一篇:必须指定分析器类型或分词器。
下一篇:必须指定一个逆向关系