7 ms·
Internal iterators are just syntactic sugar for a higher-order function. As long as Rust has higher-order functions, internal iteration will be possible (just n
by rntz 13y ago
Internal iterators are just syntactic sugar for a higher-order function. As long as Rust has higher-order functions, internal iteration will be possible (just not "the standard way to do things").
Edit: In fact, this is true of any language with higher-order functions. For example, this is how you'd define a Rust-style internal iterator for lists in Racket scheme:
(define (iter l f)
(when (and (pair? l) (f (car l)))
(iter (cdr l) f)))
And how you'd use it:
;; displays elements of the list, up to and including the first one >= 3
(iter '(1 2 3 4) (lambda (x) (display x) (< x 3)))
- kzrdude 13y agobut the `break`/`return` syntactic sugar inside for-loop closures will be lost.
- rntz 13y agoYes. You might be able to get something like it back with Rust's macro system, though. But it's probably better just to use the new syntax & iteration protocol, assuming the Rust folks come up with something reasonable.
- kibwen 13y agoTo be fair, it wasn't so much "syntactic sugar" as it was "a compiler hack that was rarely used and didn't even suffice for a large class of cases involving borrowed pointers".
- dbaupp 13y agoAnd, it is an incorrect compiler hack (https://github.com/mozilla/rust/issues/6675 https://github.com/mozilla/rust/issues/6675), but that's not saying much at the moment.