如何通过克隆将返回&T
的迭代器转换为返回T
的迭代器?
最小可重现示例:
考虑一个由ndarray
提供的迭代器,它返回&Value
对象。定义一个函数,它返回Value
对象的迭代器。
#[derive(Clone)]
struct Value;
pub trait IterValue<I: Iterator<Item = Value>> {
fn iter_value(&self) -> I;
}
impl<'cellref, 'cell: 'cellref>
IterValue<
std::iter::Map<
ndarray::iter::Iter<'cellref, Value, ndarray::Dim<[usize; 1]>>,
fn(&Value) -> Value,
>,
> for ndarray::Array1<Value>
{
fn iter_value(
&self,
) -> std::iter::Map<
ndarray::iter::Iter<'cellref, Value, ndarray::Dim<[usize; 1]>>,
fn(&Value) -> Value,
> {
self.iter().map(|val| val.clone())
}
}
// ... impl for ndarrays of other dimensions ...
// ... and possibly Array<T> for other T that will be converted to Value ...
字符串
错误输出:
error: lifetime may not live long enough
--> src\lib.rs:22:9
|
8 | impl<'cellref, 'cell: 'cellref>
| -------- lifetime `'cellref` defined here
...
17 | &self,
| - let's call the lifetime of this reference `'1`
...
22 | self.iter().map(|val| val.clone())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method was supposed to return data with lifetime `'cellref` but it is returning data with lifetime `'1`
|
help: consider adding 'move' keyword before the nested closure
|
22 | self.iter().map(move |val| val.clone())
| ++++
型
1条答案
按热度按时间fsi0uk1n1#
你的代码对于一个已经解决的问题来说是多余的。
每个
Iterator<Item = &T>
都可以通过.cloned()
转换为Iterator<Item = T>
。(当然,如果T
是Clone
)就像这样:
个字符