I'm a Rust newbie and I'm trying to figure out what the best way to do the following in Rust is:
struct ThingIterator {
current: String,
stop: String,
}
impl Iterator for ThingIterator {
type Item = &str;
fn next(&mut self) -> Option<&str> {
if self.current == self.stop {
return None;
}
// For testing
self.current = self.stop;
Some(&self.current)
}
}
fn main() {
let pi = ThingIterator {
current: String::from("Ask"),
stop: String::from("Zoo"),
};
println!("Number of things={}", pi.count());
}
My error is:
error[E0106]: missing lifetime specifier
--> src/main.rs:7:17
|
7 | type Item = &str;
| ^ expected lifetime parameter
error: aborting due to previous error
This makes sense, I need to specify how long the reference returned from next() is going to be valid. I'm guessing for the function itself it's fine as the lifetime is elissed (not sure about the conjugation of elission) - but I somehow need to define the lifetime of the "type Item = &str" row.
In my case it will be valid as long as "current" is valid, i.e. the same lifetime as "self".
I haven't seen anything in the Rust book or other documentation that helps me figure out this case.
P.S. Sorry if I'm butchering the nomenclature, I'm very much new to Rust. Thanks