18 releases (6 breaking)
new 0.7.0 | Feb 8, 2025 |
---|---|
0.6.2 | Feb 8, 2025 |
0.5.5 | Feb 4, 2025 |
0.5.0 | Jan 22, 2025 |
0.1.0 | Jan 9, 2025 |
#359 in Rust patterns
1,434 downloads per month
61KB
1K
SLoC
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);