A look at Rust from 2012
33 points by ohrv
33 points by ohrv
The question about Rust’s switch from internal to external iterators sent me looking. Sadly a lot of links from back then are broken, but these two are pertinent:
I have no special knowledge about it, but I remember boats mentioned this mailing list post that summarized it:
To reiterate the benefits of the external iteration protocol:
- It's generic and works well with traits, so functions can be written to work on any arbitrary
Iterator<A>. Most adaptors can work for any typeA, whether it's a value, reference or mutable reference.- Iteration state is an object, so iterators can be interleaved. This is required for a generic zip, merge, union, intersect, etc. and is often useful in an ad-hoc fashion to consume only some of an iterator without losing it.
- In the future, Rust can have generators using a
yieldstatement like C#, compiling down to a fast state machine without requiring context switches, virtual functions or even closures. This would eliminate the difficulty of coding recursive traversals by-hand with external iterators.
It's interesting that Go went in the opposite direction and the early version of iterators was func(func(T) bool) bool, but the final version used in the current version of Go is func(func(T) bool) (no outer bool return value). For cases when you need the outer bool, you end up just creating a hidden func, eg https://github.com/golang/net/blob/master/html/iter.go#L41-L56