In order to implement a iterator for a struct that has a slice with a lifetime, I had to do
struct S<'a, T> {
inner: &'a mut [T],
}
impl<'a, T> Iterator for S<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
let temp = std::mem::take(&mut self.inner);
let (first, rest) = temp.split_first_mut()?;
self.inner = rest;
Some(first)
}
}
While writing this code, i realized lifetime enforces additional memory copy(mem::take) that would be unnecessary in C or C++. Am i missing something here or is this a small price that rust pays for the lifetime?