Skip to content

Commit b512720

Browse files
authored
Rollup merge of #94587 - JKAnderson409:issue-90107-fix, r=scottmcm
Document new recommended use of `FromIterator::from_iter` #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.
2 parents b413745 + b363f13 commit b512720

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
/// created from an iterator. This is common for types which describe a
55
/// collection of some kind.
66
///
7-
/// [`FromIterator::from_iter()`] is rarely called explicitly, and is instead
8-
/// used through [`Iterator::collect()`] method. See [`Iterator::collect()`]'s
9-
/// documentation for more examples.
7+
/// If you want to create a collection from the contents of an iterator, the
8+
/// [`Iterator::collect()`] method is preferred. However, when you need to
9+
/// specify the container type, [`FromIterator::from_iter()`] can be more
10+
/// readable than using a turbofish (e.g. `::<Vec<_>>()`). See the
11+
/// [`Iterator::collect()`] documentation for more examples of its use.
1012
///
1113
/// See also: [`IntoIterator`].
1214
///
@@ -32,6 +34,17 @@
3234
/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
3335
/// ```
3436
///
37+
/// Using [`FromIterator::from_iter()`] as a more readable alternative to
38+
/// [`Iterator::collect()`]:
39+
///
40+
/// ```
41+
/// use std::collections::VecDeque;
42+
/// let first = (0..10).collect::<VecDeque<i32>>();
43+
/// let second = VecDeque::from_iter(0..10);
44+
///
45+
/// assert_eq!(first, second);
46+
/// ```
47+
///
3548
/// Implementing `FromIterator` for your type:
3649
///
3750
/// ```

0 commit comments

Comments
 (0)