Skip to content

Remove from_iterator_instead_of_collect lint.#16923

Open
kpreid wants to merge 1 commit intorust-lang:masterfrom
kpreid:yes-from-iter
Open

Remove from_iterator_instead_of_collect lint.#16923
kpreid wants to merge 1 commit intorust-lang:masterfrom
kpreid:yes-from-iter

Conversation

@kpreid
Copy link
Copy Markdown
Contributor

@kpreid kpreid commented Apr 28, 2026

Since the 2021 edition of Rust, FromIterator is in the prelude and is intended to be used in some cases. Per RFC 3114:

The documentation for FromIterator's from_iter() method currently reads:

FromIterator::from_iter() is rarely called explicitly, and is instead used through Iterator::collect() method. See Iterator::collect()'s documentation for more examples.

However, it is reasonably common that type inferencing fails to infer the full type of the target type, in which case an explicit type annotation or turbofishing is needed (such as iter.collect::<Vec<_, _>>() -- the type of the iterator item is available, so wildcards can be used for this). In these cases, Vec::from_iter(iter) can be a more concise and readable way to spell this, which would be easier if the trait was in scope.

Currently, the from_iter_instead_of_collect lint always lints in favor of replacing FromIterator::from_iter() with Iterator::collect() when possible. #7213 is open with the intent of changing the lint to only lint when no turbofish is needed on collect(); however, no progress has been made on that since 2021. Furthermore, whether type inference succeeds can be fragile, easily changing as a program is edited. In my opinion, Rust programmers should not be steered towards programs which contain only the minimum number of type annotations and other type constraints; they should be allowed (and even sometimes encouraged) to write programs with more explicit types. There are also several long-standing suggestion-causes-error bugs.

I believe that from_iter_instead_of_collect, in its current form and perhaps in any form, does not make code better. This commit removes it.

changelog: [from_iter_instead_of_collect]: removed

Since the 2021 edition of Rust, `FromIterator` is in the prelude and is
intended to be used in at least certain cases.
Per [RFC 3114]:

> The documentation for `FromIterator`'s `from_iter()` method currently reads:
>
> > `FromIterator::from_iter()` is rarely called explicitly, and is
> > instead used through `Iterator::collect()` method. See
> > `Iterator::collect()`'s documentation for more examples.
>
> However, it is reasonably common that type inferencing fails to infer
> the full type of the target type, in which case an explicit type
> annotation or turbofishing is needed (such as
> `iter.collect::<Vec<_, _>>()` -- the type of the iterator item is
> available, so wildcards can be used for this). In these cases,
> `Vec::from_iter(iter)` can be a more concise and readable way to spell
> this, which would be easier if the trait was in scope.

Currently, the `from_iter_instead_of_collect` lint always lints in favor
of using `collect()` when possible. Issue 7213 is open with the intent
of changing the lint to only lint when no turbofish is needed on
`collect()`; however, no progress has been made on that since 2021.
Furthermore, whether type inference succeeds can be fragile, easily
changing as a program is edited. In my opinion, Rust programmers should
not be steered towards programs which contain only the minimum number
of type annotations and other type constraints; they should be allowed
(and even sometimes encouraged) to write programs with more explicit
types. There are also several long-standing suggestion-causes-error
bugs.

I believe that `from_iter_instead_of_collect`, in its current form and
perhaps in any form, does not make code better. This commit removes it.

[RFC 3114]: https://github.com/rust-lang/rfcs/blob/master/text/3114-prelude-2021.md#fromiterator
@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 28, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 28, 2026

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq

@github-actions
Copy link
Copy Markdown

Lintcheck changes for 8c034f0

Lint Added Removed Changed
clippy::from_iter_instead_of_collect 0 11 0

This comment will be updated if you push new changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

3 participants