I have the following code, that I think is pretty self-explanatory. The problem I'm facing is in the render
function:
fn render(&self) -> &'static str {
self.view.value
}
Where the compiler complains:
attempted access of field `view` on type `&Self`, but no field with that name was found`
Full code:
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());
}
}