data_structures/linked_list/
into_iter.rs

1use super::node::*;
2
3pub struct IntoIter<T>(pub Next<T>);
4
5impl<T> Iterator for IntoIter<T> {
6    type Item = T;
7
8    fn next(&mut self) -> Option<Self::Item> {
9        let current_node = std::mem::replace(&mut (*(*self).0), None);
10
11        current_node.map(|node| {
12            (*self).0 = node.next;
13            node.data
14        })
15    }
16}