【发布时间】:2020-06-03 21:55:23
【问题描述】:
我正在尝试创建一个基于参数过滤切片的迭代器。
fn dates_from_iterator_ref<'a>(
from: &'a NaiveDate,
dates: &'a [NaiveDate],
) -> impl Iterator<Item = &'a NaiveDate> {
dates.iter().filter(|&date| date >= from)
}
fn dates_from_iterator_val<'a>(
from: NaiveDate,
dates: &'a [NaiveDate],
) -> impl Iterator<Item = &'a NaiveDate> {
dates.iter().filter(|&&date| date >= from)
}
无论我将参数作为引用还是值传递,我都会得到相同的错误:
|
89 | fn dates_from_iterator_ref<'a>(
| -- lifetime `'a` defined here
...
92 | ) -> impl Iterator<Item = &'a NaiveDate> {
| ----------------------------------- opaque type requires that `from` is borrowed for `'a`
93 | dates.iter().filter(|&date| date >= from)
| ------- ^^^^ borrowed value does not live long enough
| |
| value captured here
94 | }
| - `from` dropped here while still borrowed
我怎样才能让它工作?
【问题讨论】: