Skip to content

Commit c956fe5

Browse files
author
Jeff
committed
Document new recommended use of method
1 parent 4566094 commit c956fe5

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
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, the compiler is
9+
/// sometimes unable to infer the full type of the collection. In these cases,
10+
/// [`FromIterator::from_iter()`] can be more concise and readable. 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)