struct StringIter<'a> {
current: Option<&'a String>,
list: Vec<String>,
n: usize,
}
trait Iter<'a>{
fn iter(&'a mut self);
fn method_a(&'a mut self){self.iter()}
}
impl<'a>Iter<'a> for StringIter<'a> {
fn iter(&'a mut self) {
let mut smallest =None;
for i in 0..self.n {
smallest = Some(&self.list[i]);
}
self.current = smallest;
}
}
fn main(){}
The current
field need to point to the item in list
, so I need to add lifetime to the iter
method and the Iter
trait to make it compatible with trait.
The code works, but here's my question, there're many structs need to implement the Iter
trait, which means they need the lifetime argument too, but the lifetime is only useful for the StringIter
. Is there a hack way to elide the lifetime?