sliding_windows
This crate offers an Iterator adaptor, which yields "sliding windows" over the elements returned by the wrapped iterator.
It's worth to note that it does not copy elements, which makes the code relatively performant.
The backing storage is a Vec, so this Iterator adaptor is not ideal for very large windows (>20 elements or very huge elements).
I'd happily accept a PR to implement the same functionality with a VecDeque or similar, see this issue.
Links
Install
Add this to your Cargo.toml.
[dependencies]
sliding_windows = "2.0"
Example
extern crate sliding_windows;
use ;
let mut storage: = new;
for x in .sliding_windows
// This outputs:
// [0, 1, 2]
// [1, 2, 3]
// [2, 3, 4]
For more examples please consult the Documentation.
This functionality in other languages/crates
- Ruby: #each_cons
- Python: window
- Rust (just for slices): .windows()
- Rust (for all Iterators, but copying): .slide()