iter_chunks_ext

Crates.ioiter_chunks_ext
lib.rsiter_chunks_ext
version0.1.0
created_at2025-07-20 02:54:42.270497+00
updated_at2025-07-20 02:54:42.270497+00
descriptionIterator extension for grouping items.
homepage
repositoryhttps://github.com/nossie531/iter_chunks_ext
max_upload_size
id1760715
size21,010
(nossie531)

documentation

README

iter_chunks_ext

Iterator extension for grouping items.

The author of this crate is not good at English.
Forgive me if the document is hard to read.

What is this?

This crate provides an iterator extension that supports items grouping.

Note that “Grouping” here is dependent on orders, so it is slightly different from SQL's GROUP BY effect.

Examples

use iter_chunks_ext::prelude::*;

let items = vec![("a", 0), ("a", 1), ("b", 0), ("a", 2)];
let chunks = &mut items.iter().chunks(|x| x.0);

let chunk = &mut chunks.next().unwrap();
assert_eq!(chunk.next(), Some(&("a", 0)));
assert_eq!(chunk.next(), Some(&("a", 1)));
assert_eq!(chunk.next(), None);

let chunk = &mut chunks.next().unwrap();
assert_eq!(chunk.next(), Some(&("b", 0)));
assert_eq!(chunk.next(), None);

let chunk = &mut chunks.next().unwrap();
assert_eq!(chunk.next(), Some(&("a", 2)));
assert_eq!(chunk.next(), None);

let chunk = &mut chunks.next();
assert!(chunk.is_none());

Other options

There are many crates that can group items from iterators.

Followings are some of them.

📦 itertools (Extra iterator tools)

  • chunk_by - Creates an iterator for grouping.
    (To access groups in any order requires heap memory allocation.)

📦 grouping_by (Grouping hash map creator)

  • grouping_by - Creates hash map grouped by key.
    (Heap Memory allocation is required for use of hash maps.)

Highlights

This crate is characterized by following Pros/Cons.
These Pros/Cons are two sides of the same coin.

😊 Pros

Low memory consumption. No heap memory required.
So this crate can work in core environment.

🤔 Cons

Unlike popular iterator methods, iterators and closures are required additional trait bounds.

  • Clone trait is required for iterators.
  • Copy trait is required for closures.

Note

I feel my approach is natural. But as of 2025, I cannot find same approach.
This makes me little uneasy. So, please check carefully before using.

Versions

See CHANGELOG.

Commit count: 0

cargo fmt