I'm applying a closure on the iterator and I want to use stable, so I want to return a boxed Iterator
. The obvious way to do so is the following:
struct Foo;
fn into_iterator(myvec: &Vec<Foo>) -> Box<dyn Iterator<Item = &Foo>> {
Box::new(myvec.iter())
}
This fails because the borrow checker cannot infer the appropriate lifetimes.
After some research, I've found What is the correct way to return an Iterator (or any other trait)?, which brought me to adding + 'a
:
fn into_iterator<'a>(myvec: &'a Vec<Foo>) -> Box<dyn Iterator<Item = &'a Foo> + 'a> {
Box::new(myvec.iter())
}
But I don't understand
- What this does
- And why it is needed here