【发布时间】:2015-04-11 22:17:00
【问题描述】:
我有以下代码,我认为这很不言自明。我面临的问题在于render 函数:
fn render(&self) -> &'static str {
self.view.value
}
编译器抱怨的地方:
attempted access of field `view` on type `&Self`, but no field with that name was found`
完整代码:
struct View1 {
value: &'static str,
}
struct View2 {
value: &'static str,
another_value: &'static str,
}
struct Pattern<T> {
view: T,
}
trait Renderable {
fn render(&self) -> &'static str {
self.view.value
}
}
impl <T> Renderable for Pattern <T> {}
fn patterns() -> Vec<Box<Renderable>> {
vec![
Box::new(Pattern { view: View1 { value: "x" } }),
Box::new(Pattern { view: View1 { value: "y" } }),
]
}
fn main() {
let p = patterns();
for x in p.iter() {
println!("{}", x.render());
}
}
【问题讨论】:
标签: rust