Crates.io | unzip_iter |
lib.rs | unzip_iter |
version | |
source | src |
created_at | 2025-01-09 13:02:11.658229 |
updated_at | 2025-02-04 12:57:13.800113 |
description | Unzip an iterator to iterators |
homepage | |
repository | https://github.com/kimuti-tsukai/unzip_iter |
max_upload_size | |
id | 1509918 |
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` |
size | 0 |
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.
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
.
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);