diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-03-10 19:00:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-10 19:00:06 +0100 |
| commit | b5127202b278cc87e7577cea4165b4e39c60f1e2 (patch) | |
| tree | 195004ff4868e1aa446e41bdbfe150d3ab3a76ca | |
| parent | b41374598f3274e28273a447d2d7d82a6f26c1f3 (diff) | |
| parent | b363f130698dbc55fe594155bcb4df826ffad71e (diff) | |
| download | rust-b5127202b278cc87e7577cea4165b4e39c60f1e2.tar.gz rust-b5127202b278cc87e7577cea4165b4e39c60f1e2.zip | |
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.
| -rw-r--r-- | library/core/src/iter/traits/collect.rs | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index 637d7bc4488..dee66e6e072 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -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`]. /// @@ -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: /// /// ``` |
