[英]How do I implement a non-consuming IntoIterator for a struct with an underlying collection?
[英]How to implement a non-consuming mutable std::iter::Iterator for a data structure
我有一個名為 VecCircular 的數據結構,對於 std::iter::Iterator 的非消耗不可變實現,我遵循了此處的指南。 這是我的代碼:
pub struct VecCircularIterator<'a, T> {
vec_circular: &'a VecCircular<T>,
index: usize,
}
impl<'a, T> std::iter::IntoIterator for &'a VecCircular<T> {
type Item = &'a T;
type IntoIter = VecCircularIterator<'a, T>;
fn into_iter(self) -> Self::IntoIter {
VecCircularIterator {
vec_circular: &self,
index: self.front_index,
}
}
}
impl<'a, T> std::iter::Iterator for VecCircularIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
if self.index == self.vec_circular.rear_index || self.vec_circular.empty() {
return None;
} else {
let item = &self.vec_circular[self.index];
self.index = (self.index + 1) % self.vec_circular.capacity;
return Some(item);
}
}
}
但是當我嘗試將該實現更改為可變實現時:
pub struct VecCircularIterator<'a, T> {
vec_circular: &'a mut VecCircular<T>,
index: usize,
}
impl<'a, T> std::iter::IntoIterator for &'a VecCircular<T> {
type Item = &'a T;
type IntoIter = VecCircularIterator<'a, T>;
fn into_iter(self) -> Self::IntoIter {
VecCircularIterator {
vec_circular: &mut self,
index: self.front_index,
}
}
}
impl<'a, T> std::iter::Iterator for VecCircularIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
if self.index == self.vec_circular.rear_index || self.vec_circular.empty() {
return None;
} else {
let item = &self.vec_circular[self.index];
self.index = (self.index + 1) % self.vec_circular.capacity;
return Some(item);
}
}
}
我收到以下錯誤:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/queue/mod.rs:143:25
|
143 | let item = &self.vec_circular[self.index];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 139:5...
--> src/queue/mod.rs:139:5
|
139 | / fn next(&mut self) -> Option<&'a T> {
140 | | if self.index == self.vec_circular.rear_index || self.vec_circular.empty() {
141 | | return None;
142 | | } else {
... |
146 | | }
147 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/queue/mod.rs:143:25
|
143 | let item = &self.vec_circular[self.index];
| ^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 137:6...
--> src/queue/mod.rs:137:6
|
137 | impl<'a, T> std::iter::Iterator for VecCircularIterator<'a, T> {
| ^^
note: ...so that the types are compatible
--> src/queue/mod.rs:139:41
|
139 | fn next(&mut self) -> Option<&'a T> {
| _________________________________________^
140 | | if self.index == self.vec_circular.rear_index || self.vec_circular.empty() {
141 | | return None;
142 | | } else {
... |
146 | | }
147 | | }
| |_____^
= note: expected `std::option::Option<&'a T>`
found `std::option::Option<&T>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
error: could not compile `rudac`.
我對 rust 生命周期參數有點動搖,我不知道該怎么做。
迭代器不能從自身內部產生借來的值。 否則, next的 API 聲明必須將Self::Item
與生命周期綁定到self
。
您可以產生值而不是引用,例如,像這樣的東西(但是您的代碼示例不完整(缺少 VecCircular),因此很難猜測什么是做這件事的好方法):
impl<T> std::iter::Iterator for VecCircularIterator<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
if self.index == self.vec_circular.rear_index || self.vec_circular.empty() {
return None;
} else {
let item = self.vec_circular[self.index];
self.index = (self.index + 1) % self.vec_circular.capacity;
return item;
}
}
}
另請注意,您的into_iter
方法存在問題。 into_iter
消耗 self 因此,如果您分配對 vec_circular 的引用,它的壽命將不夠長(一旦into_iter
返回,它就會退出 scope )。
順便提一句。 因為看起來您正在自己實現一個隊列,所以您可能也對標准庫中的VecDeque感興趣。 它還提供了Iter ,它可以產生引用。 它通過不擁有 VecDeque 本身來做到這一點,而只是從中借用一個切片。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.