Skip to content

Suggest using -> impl Trait when returning type parameter -> T where T: Trait #85991

Closed
@estebank

Description

@estebank

Some newcomers are confused by impl Trait in return position because they have first internalized impl Trait as an alias for type parameters in argument position. Because of this, some people try the following:

trait Trait {}
impl Trait for () {}

// this works
fn foo() -> impl Trait {
    ()
}

// this doesn't
fn bar<T: Trait>() -> T {
    ()
}

We don't currently point them in the right direction, like we do in other cases.

Took inspiration from https://twitter.com/JaapRood/status/1400782159812628482

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-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