Here, the compiler complains that impl Foo
may not live long enough:
struct VecWrapper(Vec<Box<dyn Foo>>);
impl VecWrapper {
fn push(&mut self, item: impl Foo) {
self.0.push(Box::new(item))
}
}
trait Foo {}
This is not what I expect, because item
should be moved into the Box
. The compiler recommends adding a static lifetime:
|
4 | fn push(&mut self, item: impl Foo + 'static) {
| +++++++++
In my case, item
and VecWrapper
are created at runtime for error handling, so I would like to avoid using lifetimes here.