Skip to content

better error message when ? is applied to collect() #49391

Closed
@nikomatsakis

Description

@nikomatsakis

This code:

fn main() -> Result<(), ()> {
    vec![Ok(2)].into_iter().collect()?;
    Ok(())
}

gives a terrible error:

error[E0284]: type annotations required: cannot resolve `<_ as std::ops::Try>::Ok == _`
 --> src/main.rs:4:5
  |
4 |     vec![Ok(2)].into_iter().collect()?;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

The solution is to do .collect::<Result<Vec<_>,_>>()? (playground)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions