Skip to content

Commit 9b2f085

Browse files
committed
Improve Iterator::intersperse_ docs
1 parent 9528988 commit 9b2f085

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

library/core/src/iter/adapters/intersperse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::Peekable;
22

33
/// An iterator adapter that places a separator between all elements.
44
///
5-
/// This `struct` is created by [`Iterator::intersperse`]. See it's documentation
5+
/// This `struct` is created by [`Iterator::intersperse`]. See its documentation
66
/// for more information.
77
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
88
#[derive(Debug, Clone)]
@@ -59,7 +59,7 @@ where
5959

6060
/// An iterator adapter that places a separator between all elements.
6161
///
62-
/// This `struct` is created by [`Iterator::intersperse_with`]. See it's
62+
/// This `struct` is created by [`Iterator::intersperse_with`]. See its
6363
/// documentation for more information.
6464
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
6565
pub struct IntersperseWith<I, G>

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,22 @@ pub trait Iterator {
571571

572572
/// Places a copy of `separator` between all elements.
573573
///
574+
/// In case the separator does not implement [`Clone`] or needs to be
575+
/// computed every time, use [`intersperse_with`].
576+
///
574577
/// # Examples
575578
///
576579
/// Basic usage:
577580
///
578581
/// ```
579582
/// #![feature(iter_intersperse)]
580583
///
581-
/// let hello = ["Hello", "World"].iter().copied().intersperse(" ").collect::<String>();
582-
/// assert_eq!(hello, "Hello World");
584+
/// let hello = ["Hello", "World", "!"].iter().copied().intersperse(" ").collect::<String>();
585+
/// assert_eq!(hello, "Hello World !");
583586
/// ```
587+
///
588+
/// [`Clone`]: crate::clone::Clone
589+
/// [`intersperse_with`]: Iterator::intersperse_with
584590
#[inline]
585591
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
586592
fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
@@ -600,11 +606,13 @@ pub trait Iterator {
600606
/// ```
601607
/// #![feature(iter_intersperse)]
602608
///
603-
/// let src = ["Hello", "to", "all", "people"].iter().copied();
604-
/// let mut separator = [" ❤️ ", " 😀 "].iter().copied().cycle();
609+
/// let src = ["Hello", "to", "all", "people", "!!"].iter().copied();
610+
///
611+
/// let mut happy_emojis = [" ❤️ ", " 😀 "].iter().copied();
612+
/// let separator = || happy_emojis.next().unwrap_or(" 🦀 ");
605613
///
606-
/// let result = src.intersperse_with(|| separator.next().unwrap()).collect::<String>();
607-
/// assert_eq!(result, "Hello ❤️ to 😀 all ❤️ people");
614+
/// let result = src.intersperse_with(separator).collect::<String>();
615+
/// assert_eq!(result, "Hello ❤️ to 😀 all 🦀 people 🦀 !!");
608616
/// ```
609617
#[inline]
610618
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]

0 commit comments

Comments
 (0)