diff options
| author | alexey semenyuk <alexsemenyuk88@gmail.com> | 2024-08-03 14:45:35 +0500 |
|---|---|---|
| committer | alexey semenyuk <alexsemenyuk88@gmail.com> | 2024-08-03 19:28:21 +0500 |
| commit | 35dcc9bbfa32a070386315cdfcc73da658d0b87e (patch) | |
| tree | afc1f531096915bc4f61e26dcca60248c3c3a645 | |
| parent | 0347280d5f4e672542d9f87f40798279cf9dcfe4 (diff) | |
| download | rust-35dcc9bbfa32a070386315cdfcc73da658d0b87e.tar.gz rust-35dcc9bbfa32a070386315cdfcc73da658d0b87e.zip | |
Add clarification for from_iter_instead_of_collect
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 4c530031581..1a7fda6a364 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1892,7 +1892,9 @@ declare_clippy_lint! { /// trait. /// /// ### Why is this bad? - /// It is recommended style to use collect. See + /// If it's needed to create a collection from the contents of an iterator, the `Iterator::collect(_)` + /// method is preferred. However, when it's needed to specify the container type, + /// `Vec::from_iter(_)` can be more readable than using a turbofish (e.g. `_.collect::<Vec<_>>()`). See /// [FromIterator documentation](https://doc.rust-lang.org/std/iter/trait.FromIterator.html) /// /// ### Example @@ -1911,6 +1913,14 @@ declare_clippy_lint! { /// /// assert_eq!(v, vec![5, 5, 5, 5, 5]); /// ``` + /// but prefer to use + /// ```no_run + /// let numbers: Vec<i32> = FromIterator::from_iter(1..=5); + /// ``` + /// instead of + /// ```no_run + /// let numbers = (1..=5).collect::<Vec<_>>(); + /// ``` #[clippy::version = "1.49.0"] pub FROM_ITER_INSTEAD_OF_COLLECT, pedantic, |
