May 2019
Intermediate to advanced
698 pages
17h 21m
English
Looking at the list without consuming it is an iterator's job (see the info box), which—in Rust as well as in most other languages—is a simple implementation of an interface or trait. In fact, this is so common that the Rust docs have a great article (https://doc.rust-lang.org/std/iter/index.html#implementing-iterator), which is exactly what's required.
Since we are already working with heap references, the iterator can simply save an optional reference to a node and it's easy to move it forward and backward:
pub struct ListIterator { current: Link,}impl ListIterator { fn new(start_at: Link) -> ListIterator { ListIterator { current: start_at, } }}
As the documentation states, a for loop uses two traits: Iterator and IntoIterator ...
Read now
Unlock full access