Skip to content

Commit eb7b676

Browse files
committed
Auto merge of rust-lang#13208 - alex-semenyuk:fix_doc_from_iter_instead_of_collect, r=llogiq
Add clarification for from_iter_instead_of_collect Close rust-lang#13147 As mentioned at rust-lang#13147 we should prefer to use collect depends on situation so clarify this at documentation and provide examples this cases. changelog: none
2 parents 0347280 + 35dcc9b commit eb7b676

File tree

1 file changed

+11
-1
lines changed
  • clippy_lints/src/methods

1 file changed

+11
-1
lines changed

clippy_lints/src/methods/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,9 @@ declare_clippy_lint! {
18921892
/// trait.
18931893
///
18941894
/// ### Why is this bad?
1895-
/// It is recommended style to use collect. See
1895+
/// If it's needed to create a collection from the contents of an iterator, the `Iterator::collect(_)`
1896+
/// method is preferred. However, when it's needed to specify the container type,
1897+
/// `Vec::from_iter(_)` can be more readable than using a turbofish (e.g. `_.collect::<Vec<_>>()`). See
18961898
/// [FromIterator documentation](https://doc.rust-lang.org/std/iter/trait.FromIterator.html)
18971899
///
18981900
/// ### Example
@@ -1911,6 +1913,14 @@ declare_clippy_lint! {
19111913
///
19121914
/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
19131915
/// ```
1916+
/// but prefer to use
1917+
/// ```no_run
1918+
/// let numbers: Vec<i32> = FromIterator::from_iter(1..=5);
1919+
/// ```
1920+
/// instead of
1921+
/// ```no_run
1922+
/// let numbers = (1..=5).collect::<Vec<_>>();
1923+
/// ```
19141924
#[clippy::version = "1.49.0"]
19151925
pub FROM_ITER_INSTEAD_OF_COLLECT,
19161926
pedantic,

0 commit comments

Comments
 (0)