3

Suggest `_` for missing generic arguments in turbofish by kornelski · Pull Reque...

 4 weeks ago
source link: https://github.com/rust-lang/rust/pull/122651
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Conversation

Contributor

The compiler may suggest unusable generic type names for missing generic arguments in an expression context:

fn main() {
    (0..1).collect::<Vec>()
}

help: add missing generic argument

 (0..1).collect::<Vec<T>>()

but T is not a valid name in this context, and this suggestion won't compile.

I've changed it to use _ inside method calls (turbofish), so it will suggest (0..1).collect::<Vec<_>>() which may compile.

It's possible that the suggested _ will be ambiguous, but there is very extensive E0283 that will help resolve that, which is more helpful than a basic "cannot find type T in this scope" users would get otherwise.

Out of caution to limit scope of the change I've limited it to just turbofish, but I suspect _ could be the better choice in more cases. Perhaps in all expressions?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK