Skip to content

Commit 54f3578

Browse files
authoredJan 27, 2022
Rollup merge of #92899 - cameron1024:zip-docs, r=dtolnay
Mention std::iter::zip in Iterator::zip docs Closes #91960 I'm not sure about the wording. I think it's alright, but happy to change.
2 parents 4af3930 + 857ea1e commit 54f3578

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
 

‎library/core/src/iter/traits/iterator.rs

+36
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,44 @@ pub trait Iterator {
515515
/// assert_eq!((2, 'o'), zipper[2]);
516516
/// ```
517517
///
518+
/// If both iterators have roughly equivalent syntax, it may me more readable to use [`zip`]:
519+
///
520+
/// ```
521+
/// use std::iter::zip;
522+
///
523+
/// let a = [1, 2, 3];
524+
/// let b = [2, 3, 4];
525+
///
526+
/// let mut zipped = zip(
527+
/// a.into_iter().map(|x| x * 2).skip(1),
528+
/// b.into_iter().map(|x| x * 2).skip(1),
529+
/// );
530+
///
531+
/// assert_eq!(zipped.next(), Some((4, 6)));
532+
/// assert_eq!(zipped.next(), Some((6, 8)));
533+
/// assert_eq!(zipped.next(), None);
534+
/// ```
535+
///
536+
/// compared to:
537+
///
538+
/// ```
539+
/// # let a = [1, 2, 3];
540+
/// # let b = [2, 3, 4];
541+
/// #
542+
/// let mut zipped = a
543+
/// .into_iter()
544+
/// .map(|x| x * 2)
545+
/// .skip(1)
546+
/// .zip(b.into_iter().map(|x| x * 2).skip(1));
547+
/// #
548+
/// # assert_eq!(zipped.next(), Some((4, 6)));
549+
/// # assert_eq!(zipped.next(), Some((6, 8)));
550+
/// # assert_eq!(zipped.next(), None);
551+
/// ```
552+
///
518553
/// [`enumerate`]: Iterator::enumerate
519554
/// [`next`]: Iterator::next
555+
/// [`zip`]: crate::iter::zip
520556
#[inline]
521557
#[stable(feature = "rust1", since = "1.0.0")]
522558
fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter>

0 commit comments

Comments
 (0)
Please sign in to comment.