3 ms·
Yes there is conditional logic, but normally only the minimum necessary. The iteration protocol is just a trait: trait Iterator<A> { fn next(&mut self
by dbaupp 12y ago
Yes there is conditional logic, but normally only the minimum necessary.
The iteration protocol is just a trait:
trait Iterator<A> {
fn next(&mut self) -> Option<A>;
}
That is, "an iterator" is an object that has a next method yielding an optional value, with a missing value indicating the end of iteration. This is conventionally used statically dispatched, and the next method will normally be inlined, so the compiler can optimise it well.
(In particular iterators over simple data structures like vectors optimise to the same code as the equivalent in C/C++, modulo one small bug where LLVM doesn't yet fully understand the nonnullability of Rust's references.)