Now, since the requirement was also to go back, the iterator needs to go both ways. One easy way is to simply add a function to the structure that is called reverse(), but that would not integrate well and would require developers to read up on this API, and it creates additional work, since the forward/backward iterators are separate.
Rust's standard library offers an interesting concept for this: DoubleEndedIterator. Implementing this trait will provide the ability to reverse an iterator in a standardized way by offering a next_back() function to get the previous value—with the doubly linked list, this is only a matter of which property gets set to the current item! Therefore, both iterators share a large chunk of the code:
impl DoubleEndedIterator for ListIterator {
fn next_back(&mut self) -> Option<String> {
let current = &self.current;
let mut result = None;
self.current = match current {
Some(ref current) =...