Skip to content

Use try_for_each instead of map collect #6208

@camsteffen

Description

@camsteffen

What it does

Suggest using try_for_each instead of map(...).collect() whenever possible.

More specifically, detect code in the following form:

iter.map(/* anything that returns Result<(), _> */).collect::<Result<(), _>>()

Categories (optional)

  • Kind: clippy::style

What is the advantage of the recommended code over the original code

It is easier to read and more idiomatic

Drawbacks

None.

Example

iter.map(|t| Err(t)).collect::<Result<(), _>>()

Could be written as:

iter.try_for_each(|t| Err(t))

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions