我在迭代器上应用闭包,我想使用stable,所以我想返回一个装箱的Iterator
.显而易见的方法是:
struct Foo;
fn into_iterator(myvec: &Vec<Foo>) -> Box<dyn Iterator<Item = &Foo>> {
Box::new(myvec.iter())
}
这会失败,因为借阅判断器无法推断适当的生存期.
经过一些研究,我发现了What is the correct way to return an Iterator (or any other trait)?个,这使我增加了+ 'a
个:
fn into_iterator<'a>(myvec: &'a Vec<Foo>) -> Box<dyn Iterator<Item = &'a Foo> + 'a> {
Box::new(myvec.iter())
}
但我不明白
- 这是干什么的
- 为什么这里需要它