@@ -569,9 +569,10 @@ pub trait Iterator {
569
569
Zip :: new ( self , other. into_iter ( ) )
570
570
}
571
571
572
- /// Places a copy of `separator` between all elements.
572
+ /// Creates a new iterator which places a copy of `separator` between adjacent
573
+ /// items of the original iterator.
573
574
///
574
- /// In case the separator does not implement [`Clone`] or needs to be
575
+ /// In case ` separator` does not implement [`Clone`] or needs to be
575
576
/// computed every time, use [`intersperse_with`].
576
577
///
577
578
/// # Examples
@@ -581,6 +582,19 @@ pub trait Iterator {
581
582
/// ```
582
583
/// #![feature(iter_intersperse)]
583
584
///
585
+ /// let mut a = [0, 1, 2].iter().intersperse(&100);
586
+ /// assert_eq!(a.next(), Some(&0)); // The first element from `a`.
587
+ /// assert_eq!(a.next(), Some(&100)); // The separator.
588
+ /// assert_eq!(a.next(), Some(&1)); // The next element from `a`.
589
+ /// assert_eq!(a.next(), Some(&100)); // The separator.
590
+ /// assert_eq!(a.next(), Some(&2)); // The last element from `a`.
591
+ /// assert_eq!(a.next(), None); // The iterator is finished.
592
+ /// ```
593
+ ///
594
+ /// `intersperse` can be very useful to join an iterator's items using a common element:
595
+ /// ```
596
+ /// #![feature(iter_intersperse)]
597
+ ///
584
598
/// let hello = ["Hello", "World", "!"].iter().copied().intersperse(" ").collect::<String>();
585
599
/// assert_eq!(hello, "Hello World !");
586
600
/// ```
@@ -597,7 +611,16 @@ pub trait Iterator {
597
611
Intersperse :: new ( self , separator)
598
612
}
599
613
600
- /// Places an element generated by `separator` between all elements.
614
+ /// Creates a new iterator which places an item generated by `separator`
615
+ /// between adjacent items of the original iterator.
616
+ ///
617
+ /// The closure will be called exactly once each time an item is placed
618
+ /// between two adjacent items from the underlying iterator; specifically,
619
+ /// the closure is not called if the underlying iterator yields less than
620
+ /// two items and after the last item is yielded.
621
+ ///
622
+ /// If the iterator's item implements [`Clone`], it may be easier to use
623
+ /// [`intersperse`].
601
624
///
602
625
/// # Examples
603
626
///
@@ -606,14 +629,36 @@ pub trait Iterator {
606
629
/// ```
607
630
/// #![feature(iter_intersperse)]
608
631
///
632
+ /// #[derive(PartialEq, Debug)]
633
+ /// struct NotClone(usize);
634
+ ///
635
+ /// let v = vec![NotClone(0), NotClone(1), NotClone(2)];
636
+ /// let mut it = v.into_iter().intersperse_with(|| NotClone(99));
637
+ ///
638
+ /// assert_eq!(it.next(), Some(NotClone(0))); // The first element from `v`.
639
+ /// assert_eq!(it.next(), Some(NotClone(99))); // The separator.
640
+ /// assert_eq!(it.next(), Some(NotClone(1))); // The next element from `v`.
641
+ /// assert_eq!(it.next(), Some(NotClone(99))); // The separator.
642
+ /// assert_eq!(it.next(), Some(NotClone(2))); // The last element from from `v`.
643
+ /// assert_eq!(it.next(), None); // The iterator is finished.
644
+ /// ```
645
+ ///
646
+ /// `intersperse_with` can be used in situations where the separator needs
647
+ /// to be computed:
648
+ /// ```
649
+ /// #![feature(iter_intersperse)]
650
+ ///
609
651
/// let src = ["Hello", "to", "all", "people", "!!"].iter().copied();
610
652
///
653
+ /// // The closure mutably borrows it's context to generate an item.
611
654
/// let mut happy_emojis = [" ❤️ ", " 😀 "].iter().copied();
612
655
/// let separator = || happy_emojis.next().unwrap_or(" 🦀 ");
613
656
///
614
657
/// let result = src.intersperse_with(separator).collect::<String>();
615
658
/// assert_eq!(result, "Hello ❤️ to 😀 all 🦀 people 🦀 !!");
616
659
/// ```
660
+ /// [`Clone`]: crate::clone::Clone
661
+ /// [`intersperse`]: Iterator::intersperse
617
662
#[ inline]
618
663
#[ unstable( feature = "iter_intersperse" , reason = "recently added" , issue = "79524" ) ]
619
664
fn intersperse_with < G > ( self , separator : G ) -> IntersperseWith < Self , G >
0 commit comments