I'm making an iterator in rust. In the next() method I want to extract next_element as current_element which will be returned, and create new Term instance and set it as next_element:
pub struct Term { pub coefficient: f64, pub index: i32, pub exponent: f64, } pub struct SeriesIterator { next_element: Term, } impl Iterator for SeriesIterator { type Item = Term; fn next(&mut self) -> Option<Self::Item> { let current_element = self.next_element; self.next_element = Term { coefficient: 1.0, index: current_element.index + 1, exponent: f64::from(current_element.index + 1), }; Some(current_element) } } How to move ownership of next_element into current_element which will finally be moved outside with the return of next()?
cannot move out of `self.next_element` which is behind a mutable reference | | let current_element = self.next_element; | ^^^^^^^^^^^^^^^^