I'm trying to create a VecDeque of structs that all implement an Animal
trait. This code works, but I don't understand why adding ' static
fixes it and how to make it use 'a
instead.
pub trait Animal {
fn says(self) -> Option<String>;
}
use std::collections::VecDeque;
pub struct Zoo {
list: VecDeque<Box<dyn Animal>>,
}
impl Zoo {
pub fn new() -> Zoo {
Zoo {
list: VecDeque::new(),
}
}
pub fn add<T>(&mut self, animal: T)
where
T: Animal + 'static,
{
self.list.push_back(Box::new(animal));
}
}
Two questions:
- Could someone please explain how to use
'a
properly and how this would work / what it would mean? And also I guess why I even need a lifetime here (is it because I'm using Box)? - I'm also confused why I have to use
#[path="..."]
since without it, it asks me to move the file tosrc/lib/animal.rs
but when I move it, that still doesn't work.