Skip to content

Commit 7b5af57

Browse files
committed
Add docs for FromIterator<(AE, BE)> for (A, B)
1 parent 03862a5 commit 7b5af57

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

library/core/src/iter/traits/collect.rs

+19
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ pub trait FromIterator<A>: Sized {
150150
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
151151
}
152152

153+
/// This implementation turns an iterator of tuples into a tuple of types which implement
154+
/// [`Default`] and [`Extend`].
155+
///
156+
/// This is similar to [`Iterator::unzip`], but is also composable with other [`FromIterator`]
157+
/// implementations:
158+
///
159+
/// ```rust
160+
/// # fn main() -> Result<(), core::num::ParseIntError> {
161+
/// let string = "1,2,123,4";
162+
///
163+
/// let (numbers, lengths): (Vec<_>, Vec<_>) = string
164+
/// .split(',')
165+
/// .map(|s| s.parse().map(|n: u32| (n, s.len())))
166+
/// .collect::<Result<_, _>>()?;
167+
///
168+
/// assert_eq!(numbers, [1, 2, 123, 4]);
169+
/// assert_eq!(lengths, [1, 1, 3, 1]);
170+
/// # Ok(()) }
171+
/// ```
153172
#[stable(feature = "from_iterator_for_tuple", since = "CURRENT_RUSTC_VERSION")]
154173
impl<A, B, AE, BE> FromIterator<(AE, BE)> for (A, B)
155174
where

0 commit comments

Comments
 (0)