I've defined a struct which only has a slice of element of type T.
pub struct MyStruct<'a, T> {
slice: &'a mut [T],
}
I want to define an Iterator
for MyStruct
which yields a MyStruct
with a slice which has 10 elements.
impl<'a, E> Iterator for MyStruct<'a, E> {
type Item = MyStruct<'a, E>;
fn next(&mut self) -> Option<Self::Item> {
if self.slice.len() < 10 {
return None;
} else {
let (head, tail) = self.slice.split_at_mut(10);
self.slice = tail;
Some(MyStruct { slice: head })
}
}
}
But unfortunately I'm lifetime issues.
error: lifetime may not live long enough
--> src/lib.rs:12:32
|
5 | impl<'a, E> Iterator for MyStruct<'a, E> {
| -- lifetime `'a` defined here
...
8 | fn next(&mut self) -> Option<Self::Item> {
| - let's call the lifetime of this reference `'1`
...
12 | let (head, tail) = self.slice.split_at_mut(10);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'a`
I'm not an expert in lifetime and would appreciate it if someone could point out to me what I'm doing incorrectly here.