Skip to content

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

Closed
@estebank

Description

@estebank
Contributor

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
P-lowLow priority
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-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-papercutDiagnostics: An error or lint that needs small tweaks.
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
on Jun 4, 2021
Noratrieb

Noratrieb commented on Sep 28, 2021

@Noratrieb
Member

@rustbot claim

10 remaining items

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

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

      Participants

      @estebank@jackh726@Noratrieb

      Issue actions

        Suggest using `-> impl Trait` when returning type parameter `-> T where T: Trait` · Issue #85991 · rust-lang/rust