I’ve just spent hours trying to figure out something rather simple and I must be missing something very obvious:
I have a struct with a field
notifiers: Vec<Box<dyn Notifier>>.
Now I would like to write an iterator that goes thru the Vec, looks up some data and returns a references to some of those
Notifier objects (along with some other data – it basically checks whether it eligible for a contact).
Now, if I pass a reference of
notifiers into the iterator, I run into lifetime issues immediately:
does not live long enough, or
cannot infer an appropriate lifetime due to conflicting requirements…I’ve seen them all.
So an obvious (albeit not very efficient) solution would be to just clone the Vec. But of course, trying to make the Notifier trait
Clone, leads to the infamous
cannot be made into an object because Clone is also Sized.
So I can’t hold a reference to it and I also can’t clone it…
I’ve re-read the chapters on traits, iterators, & lifetimes in all my Rust books, but something is blocked in my head. What would be the right approach for a problem like that?