Skip to content

Commit f27758e

Browse files
committed
mention std::iter::zip in Iterator::zip docs
1 parent 02c9e73 commit f27758e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

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

0 commit comments

Comments
 (0)