rust-lang-nursery / lazy-static.rs

A small macro for defining lazy evaluated static variables in Rust.
Apache License 2.0
1.9k stars 111 forks source link

help newbie #137

Closed AlbanMinassian closed 5 years ago

AlbanMinassian commented 5 years ago

i try to set global this examples https://doc.rust-lang.org/1.30.0/book/second-edition/ch17-02-trait-objects.html with lazy_statyc

pub trait Draw {
    fn draw(&self);
}
pub struct Screen<T: Draw> {
    pub components: Vec<T>,
}
impl<T> Screen<T>
    where T: Draw {
    pub fn run(&self) {
        for component in self.components.iter() {
            component.draw();
        }
    }
}

lazy_static! {
  static ref EXAMPLE: Screen = Screen{ components: vec![] };
}

and i have this error

120 |     static ref EXAMPLE: Screen = Screen{ components: vec![] };
    |                         ^^^^^^ expected 1 type argument

i don't understand which argument is expected Can you help me ? Alban

KodrAus commented 5 years ago

Hi @AlbanMinassian :wave:

The problem here is that Rust can't figure out what type to use for T in Screen{ components: vec![] }.

Is there a specific type you want to store in that list for EXAMPLE, or do you want to store any arbitrary type that implements Draw?

KodrAus commented 5 years ago

It's been a little while now so I'll go ahead and close this one.

If you run into any more trouble with Rust please feel free to reach out on one of the community channels.