必须指定关联类型 `item` 的值(来自 trait `Draw`)
the value of the associated type `item` (from trait `Draw`) must be specified
pub trait Draw {
type item;
fn draw(&self);
}
pub struct Screen {
pub comps: Vec<Box<dyn Draw>>,
}
impl Screen {
pub fn run(&self) {
for comp in self.comps.iter() {
comp.draw();
}
}
}
我收到此错误消息:
error[E0191]: the value of the associated type `item` (from trait `Draw`) must be specified
--> src/main.rs:12:28
|
6 | type item;
| ---------- `item` defined here
...
12 | pub comps: Vec<Box<dyn Draw>>,
| ^^^^ help: specify the associated type: `Draw<item = Type>`
我该如何解决?
指定trait对象vec时需要指定item的具体类型
即
pub struct Screen {
pub comps: Vec<Box<dyn Draw<item = i64>>>,
}
pub trait Draw {
type item;
fn draw(&self);
}
pub struct Screen {
pub comps: Vec<Box<dyn Draw>>,
}
impl Screen {
pub fn run(&self) {
for comp in self.comps.iter() {
comp.draw();
}
}
}
我收到此错误消息:
error[E0191]: the value of the associated type `item` (from trait `Draw`) must be specified
--> src/main.rs:12:28
|
6 | type item;
| ---------- `item` defined here
...
12 | pub comps: Vec<Box<dyn Draw>>,
| ^^^^ help: specify the associated type: `Draw<item = Type>`
我该如何解决?
指定trait对象vec时需要指定item的具体类型
即
pub struct Screen {
pub comps: Vec<Box<dyn Draw<item = i64>>>,
}