Skip to content

Commit e5d8efc

Browse files
authored
Rollup merge of #65877 - lzutao:iter-chain-once, r=Centril
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
2 parents 5d09591 + b626448 commit e5d8efc

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/libcore/iter/traits/iterator.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ pub trait Iterator {
384384
///
385385
/// In other words, it links two iterators together, in a chain. 🔗
386386
///
387+
/// [`once`] is commonly used to adapt a single value into a chain of
388+
/// other kinds of iteration.
389+
///
387390
/// # Examples
388391
///
389392
/// Basic usage:
@@ -408,9 +411,6 @@ pub trait Iterator {
408411
/// [`Iterator`] itself. For example, slices (`&[T]`) implement
409412
/// [`IntoIterator`], and so can be passed to `chain()` directly:
410413
///
411-
/// [`IntoIterator`]: trait.IntoIterator.html
412-
/// [`Iterator`]: trait.Iterator.html
413-
///
414414
/// ```
415415
/// let s1 = &[1, 2, 3];
416416
/// let s2 = &[4, 5, 6];
@@ -425,6 +425,21 @@ pub trait Iterator {
425425
/// assert_eq!(iter.next(), Some(&6));
426426
/// assert_eq!(iter.next(), None);
427427
/// ```
428+
///
429+
/// If you work with Windows API, you may wish to convert [`OsStr`] to `Vec<u16>`:
430+
///
431+
/// ```
432+
/// #[cfg(windows)]
433+
/// fn os_str_to_utf16(s: &OsStr) -> Vec<u16> {
434+
/// use std::os::windows::ffi::OsStrExt;
435+
/// s.encode_wide().chain(std::iter::once(0)).collect()
436+
/// }
437+
/// ```
438+
///
439+
/// [`once`]: fn.once.html
440+
/// [`Iterator`]: trait.Iterator.html
441+
/// [`IntoIterator`]: trait.IntoIterator.html
442+
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
428443
#[inline]
429444
#[stable(feature = "rust1", since = "1.0.0")]
430445
fn chain<U>(self, other: U) -> Chain<Self, U::IntoIter> where

0 commit comments

Comments
 (0)