Skip to content

Commit

Permalink
Improve Iterator::intersperse_ docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslueg committed Jan 13, 2021
1 parent 9528988 commit 9b2f085
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions library/core/src/iter/adapters/intersperse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::Peekable;

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

/// An iterator adapter that places a separator between all elements.
///
/// This `struct` is created by [`Iterator::intersperse_with`]. See it's
/// This `struct` is created by [`Iterator::intersperse_with`]. See its
/// documentation for more information.
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
pub struct IntersperseWith<I, G>
Expand Down
20 changes: 14 additions & 6 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,22 @@ pub trait Iterator {

/// Places a copy of `separator` between all elements.
///
/// In case the separator does not implement [`Clone`] or needs to be
/// computed every time, use [`intersperse_with`].
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(iter_intersperse)]
///
/// let hello = ["Hello", "World"].iter().copied().intersperse(" ").collect::<String>();
/// assert_eq!(hello, "Hello World");
/// let hello = ["Hello", "World", "!"].iter().copied().intersperse(" ").collect::<String>();
/// assert_eq!(hello, "Hello World !");
/// ```
///
/// [`Clone`]: crate::clone::Clone
/// [`intersperse_with`]: Iterator::intersperse_with
#[inline]
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
Expand All @@ -600,11 +606,13 @@ pub trait Iterator {
/// ```
/// #![feature(iter_intersperse)]
///
/// let src = ["Hello", "to", "all", "people"].iter().copied();
/// let mut separator = [" ❤️ ", " 😀 "].iter().copied().cycle();
/// let src = ["Hello", "to", "all", "people", "!!"].iter().copied();
///
/// let mut happy_emojis = [" ❤️ ", " 😀 "].iter().copied();
/// let separator = || happy_emojis.next().unwrap_or(" 🦀 ");
///
/// let result = src.intersperse_with(|| separator.next().unwrap()).collect::<String>();
/// assert_eq!(result, "Hello ❤️ to 😀 all ❤️ people");
/// let result = src.intersperse_with(separator).collect::<String>();
/// assert_eq!(result, "Hello ❤️ to 😀 all 🦀 people 🦀 !!");
/// ```
#[inline]
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
Expand Down

0 comments on commit 9b2f085

Please sign in to comment.