Skip to content

Commit

Permalink
Rollup merge of rust-lang#94587 - JKAnderson409:issue-90107-fix, r=sc…
Browse files Browse the repository at this point in the history
…ottmcm

Document new recommended use of `FromIterator::from_iter`

rust-lang#90107
Most of the added prose was paraphrased from the links provided in the issue. The suggested `VecDeque` example seemed to make the point well enough so I just used that.
  • Loading branch information
matthiaskrgr authored Mar 10, 2022
2 parents b413745 + b363f13 commit b512720
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
/// created from an iterator. This is common for types which describe a
/// collection of some kind.
///
/// [`FromIterator::from_iter()`] is rarely called explicitly, and is instead
/// used through [`Iterator::collect()`] method. See [`Iterator::collect()`]'s
/// documentation for more examples.
/// If you want to create a collection from the contents of an iterator, the
/// [`Iterator::collect()`] method is preferred. However, when you need to
/// specify the container type, [`FromIterator::from_iter()`] can be more
/// readable than using a turbofish (e.g. `::<Vec<_>>()`). See the
/// [`Iterator::collect()`] documentation for more examples of its use.
///
/// See also: [`IntoIterator`].
///
Expand All @@ -32,6 +34,17 @@
/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
/// ```
///
/// Using [`FromIterator::from_iter()`] as a more readable alternative to
/// [`Iterator::collect()`]:
///
/// ```
/// use std::collections::VecDeque;
/// let first = (0..10).collect::<VecDeque<i32>>();
/// let second = VecDeque::from_iter(0..10);
///
/// assert_eq!(first, second);
/// ```
///
/// Implementing `FromIterator` for your type:
///
/// ```
Expand Down

0 comments on commit b512720

Please sign in to comment.