| Crates.io | iter-chunks |
| lib.rs | iter-chunks |
| version | 0.2.2 |
| created_at | 2022-01-15 15:08:39.501081+00 |
| updated_at | 2023-12-07 04:59:57.06516+00 |
| description | Extend Iterator with chunks |
| homepage | https://github.com/TennyZhuang/iter-chunks |
| repository | https://github.com/TennyZhuang/iter-chunks |
| max_upload_size | |
| id | 514375 |
| size | 23,962 |
Yet another crate to provides chunks method to rust Iterator.
Please read the API documentation here.
Add this to your Cargo.toml:
[dependencies]
iter-chunks = "0.2"
Currently, only while loop over Chunks is supported.
use iter_chunks::IterChunks;
let arr = [1, 1, 2, 2, 3];
let expected = [vec![1, 1], vec![2, 2], vec![3]];
let mut chunks = arr.into_iter().chunks(2);
let mut i = 0;
while let Some(chunk) = chunks.next() {
assert_eq!(chunk.collect::<Vec<_>>(), expected[i]);
i += 1;
}
itertools provides many awesome extensions, including chunks. It's really useful, but it use RefCell internally, causing it's not Send.
It's a very common usecase in async context, which requires Chunks to be Send:
async fn do_some_work(input: impl Iterator<Item = i32>) {
for chunk in input.chunks(1024) {
for v in chunk {
handle(v).await
}
do_some_flush().await
}
}
This crate implements chunks without RefCell, so Chunks is both Send and Sync. As a price, Chunks cannot implement Iterator (which can be resolved later by GAT and LendingIterator).
The lack of the Iterator implementation is very difficult to use, and the best solution is to wait for GAT and a suitable LendingIterator crate. But in the short term, we can consider providing some common methods such as nth, for_each, try_for_each, and so on.
Contributions are welcome.
Licensed under either of
at your option.