I am trying to implement an iterator, that iterates through a collection and produces mutable items (or mutable references) of this collection.
It is something, that I'd like to bring into jsonpath library (GitHub - greyblake/jsonpath-rs: JSONPath for Rust).
However I found very challenging to implement an iterator like this. I've decided to start small. I am trying to implement an iterator of mutable items that iterates through a vector of strings.
use std::marker;
struct IterMut<'a, T: 'a> {
parts: Vec<String>,
index: usize,
_marker: marker::PhantomData<&'a mut T>
}
impl<'a, T> IterMut<'a, T> {
fn new(parts: Vec<String>) -> Self {
Self { parts, index: 0, _marker: marker::PhantomData }
}
}
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut String;
fn next(&mut self) -> Option<Self::Item> {
let res = self.parts.get_mut(self.index);
self.index += 1;
res
}
}
fn main() {
let parts = vec![
"aaa".to_owned(),
"bbb".to_owned(),
];
let iter = IterMut::new(parts);
for part in iter {
part.push('!');
println!("-> {}", part);
}
}
I am getting the following errors:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:40:19
|
40 | let res = self.parts.get_mut(self.index);
| ^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 39:5...
--> src/main.rs:39:5
|
39 | / fn next(&mut self) -> Option<Self::Item> {
40 | | let res = self.parts.get_mut(self.index);
41 | | self.index += 1;
42 | | res
43 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:40:19
|
40 | let res = self.parts.get_mut(self.index);
| ^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 36:6...
--> src/main.rs:36:6
|
36 | impl<'a, T> Iterator for IterMut<'a, T> {
| ^^
= note: ...so that the types are compatible:
expected std::iter::Iterator
found std::iter::Iterator
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
error: Could not compile `it`.
Any ideas how to do this right?
Thanks in advanced.