unzip_iter

Crates.iounzip_iter
lib.rsunzip_iter
version
sourcesrc
created_at2025-01-09 13:02:11.658229
updated_at2025-02-04 12:57:13.800113
descriptionUnzip an iterator to iterators
homepage
repositoryhttps://github.com/kimuti-tsukai/unzip_iter
max_upload_size
id1509918
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(kimuti-tsukai)

documentation

README

Unzip iterators

This module provides a trait Unzip that allows splitting an iterator over tuples into two separate iterators.

The Unzip trait simplifies the process of working with iterators of tuples by providing a method unzip_iter. This method produces two independent iterators, each iterating over one side of the tuple. This can be especially useful when you need to process or collect the components of the tuples separately.

Example

use unzip_iter::Unzip;

let it = vec![(1, 2), (3, 3), (5, 4)].into_iter();
let (left, right) = it.unzip_iter();

assert!(left.eq(vec![1, 3, 5].into_iter()));
assert!(right.eq(vec![2, 3, 4].into_iter()));

If you want to splitting an iterator over tuples into more than two iterators, you can do as follows:

use unzip_iter::Unzip;

let it = vec![(1, 2, 3), (4, 5, 6), (7, 8, 9)].into_iter();

let tuple_iter = it.map(|(a, b, c)| (a, (b, c)));
let (left, right) = tuple_iter.unzip_iter();
let (middle, right) = right.unzip_iter();

assert!(left.eq(vec![1, 4, 7].into_iter()));
assert!(middle.eq(vec![2, 5, 8].into_iter()));
assert!(right.eq(vec![3, 6, 9].into_iter()));

The module also provides SyncUnzipIter for thread-safe usage via Arc and Mutex.

Performance Considerations

When calling next() multiple times in succession, it's recommended to use lock() or borrow() methods to avoid repeated locking/unlocking overhead. For example:

use unzip_iter::Unzip;

let it = vec![(1, 2), (3, 4)].into_iter();
let (left, right) = it.unzip_iter();

// More efficient way when calling next() multiple times
let mut left_guard = left.borrow(); // or lock() for SyncUnzipIter
let first = left_guard.next();
let second = left_guard.next();
drop(left_guard);
Commit count: 60

cargo fmt