3 ms·
Since Rust people are reading this. I find myself wanting to walk an iterator pairwise regularly for stuff like this and I'm using: for (cur, next) in (&
by grayrest 10y ago
Since Rust people are reading this.
I find myself wanting to walk an iterator pairwise regularly for stuff like this and I'm using:
for (cur, next) in (& vec).iter().zip(vec.iter().skip(1)) {
encoded_data.push(cur);
if cur == 0xff && next == 0x00 {
// ...
}
}
Is there a better way to write this? What I'd really like is the equivalent to Clojure's partition[1]. I know that the stdlib has windows `(partition n (dec n) seq)` and chunks `(partition n n seq)` implemented on slices but I usually want this on iterators for the lookahead-like behavior shown here.
[1] https://clojuredocs.org/clojure.core/partition https://clojuredocs.org/clojure.core/partition
- steveklabnik 10y agoI'm not 100% sure, but https://crates.io/crates/itertools https://crates.io/crates/itertools might have what you're looking for.
- elktea 10y agoI think http://bluss.github.io/rust-itertools/doc/itertools/struct.Zip.html http://bluss.github.io/rust-itertools/doc/itertools/struct.Z... would fit their use case?
- dbaupp 10y agoThe `.windows(usize)` function almost does this, but it uses a runtime width and so has to yield slices rather than tuples.
- doomrobo 10y agoIf you can afford the allocation at every iteration, the iterslide crate might work for you. https://github.com/slapresta/rust-iterslide https://github.com/slapresta/rust-iterslide