Skip to content

Commit

Permalink
Rollup merge of #65877 - lzutao:iter-chain-once, r=Centril
Browse files Browse the repository at this point in the history
doc: introduce `once` in `iter::chain` document

I find it hard to find which one to use with `chain` when I only need to
chain one value. Also [`once`][1] talks about `chain`.

[1]: https://doc.rust-lang.org/nightly/std/iter/fn.once.html
  • Loading branch information
Centril committed Oct 27, 2019
2 parents 5d09591 + b626448 commit e5d8efc
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/libcore/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ pub trait Iterator {
///
/// In other words, it links two iterators together, in a chain. 🔗
///
/// [`once`] is commonly used to adapt a single value into a chain of
/// other kinds of iteration.
///
/// # Examples
///
/// Basic usage:
Expand All @@ -408,9 +411,6 @@ pub trait Iterator {
/// [`Iterator`] itself. For example, slices (`&[T]`) implement
/// [`IntoIterator`], and so can be passed to `chain()` directly:
///
/// [`IntoIterator`]: trait.IntoIterator.html
/// [`Iterator`]: trait.Iterator.html
///
/// ```
/// let s1 = &[1, 2, 3];
/// let s2 = &[4, 5, 6];
Expand All @@ -425,6 +425,21 @@ pub trait Iterator {
/// assert_eq!(iter.next(), Some(&6));
/// assert_eq!(iter.next(), None);
/// ```
///
/// If you work with Windows API, you may wish to convert [`OsStr`] to `Vec<u16>`:
///
/// ```
/// #[cfg(windows)]
/// fn os_str_to_utf16(s: &OsStr) -> Vec<u16> {
/// use std::os::windows::ffi::OsStrExt;
/// s.encode_wide().chain(std::iter::once(0)).collect()
/// }
/// ```
///
/// [`once`]: fn.once.html
/// [`Iterator`]: trait.Iterator.html
/// [`IntoIterator`]: trait.IntoIterator.html
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn chain<U>(self, other: U) -> Chain<Self, U::IntoIter> where
Expand Down

0 comments on commit e5d8efc

Please sign in to comment.