struct Bar<'a> {
items: Vec<&'a dyn Any>,
}
impl<'a> Bar<'a> {
fn new() -> Self {
Bar { items: Vec::new() }
}
fn add<T>(&mut self, item: &'a T) {
self.items.push(item);
}
}
the parameter type
T
may not live long enough
the parameter typeT
must be valid for the static lifetime…rustcClick for full compiler diagnostic
mod.rs(45, 13): consider adding an explicit lifetime bound:: 'static
I don’t understand why the type T requires a 'static
lifetime. Now, I could argue that in some cases T
may be some anonymous type such as a closure and as a result won’t have a static lifetime. Thinking this, I change the code slightly to put a lifetime bound on T as well.
struct Bar<'a> {
items: Vec<&'a dyn Any>,
}
impl<'a> Bar<'a> {
fn new() -> Self {
Bar { items: Vec::new() }
}
fn add<T>(&mut self, item: &'a T) where T: 'a {
self.items.push(item);
}
}
This STILL results in the same error as above.