Skip to content

Suggest using Self in traits #66424

Closed
@ChrisJefferson

Description

@ChrisJefferson

I wrote the following trait: trait Area : Sized { fn f(a : Area) -> Area; }. What I meant (I've now discovered) was trait Area : Sized { fn f(a : Self) -> Self; }. However, it isn't easy to get "to that point".

Reading the pages linked to from the compiler warning, and the rust book, I didn't find anything which discussed what using the name of a trait inside the trait means -- in particular (and this was I believe the source of my confusion), it isn't like C++ where, while defining template<typename T> vector { ... }, we use vector to refer to the type we are currently defining.

I'm not suggesting a change of behaviour, but I wonder if this could be a common enough bug to say "did you mean Self?"

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`A-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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