【发布时间】:2020-05-21 04:09:08
【问题描述】:
我正在尝试实现一个简单的查找迭代器:
pub struct LookupIterMut<'a, D> {
data : &'a mut [D],
indices : &'a [usize],
i: usize
}
impl<'a, D> Iterator for LookupIterMut<'a, D> {
type Item = &'a mut D;
fn next(&mut self) -> Option<Self::Item> {
if self.i >= self.indices.len() {
None
} else {
let index = self.indices[self.i] as usize;
self.i += 1;
Some(&mut self.data[index]) // error here
}
}
}
这个想法是允许调用者连续可变地访问内部存储。但是我收到了错误cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements。
据我了解,我必须将函数签名更改为 next(&'a mut self) -> ..,但这不再是迭代器了。
我还发现我可以简单地使用原始指针,但我不确定这是否合适:
// ...
type Item = *mut D;
// ...
感谢您的帮助
【问题讨论】:
标签: rust