Description
This is a tracking issue for the explicit_generic_args_with_impl_trait
feature gate.
There has not been an RFC for this.
Steps
- Implement (Implement a
explicit_generic_args_with_impl_trait
feature gate #86176) - Adjust documentation (Add a note to the turbofish section about impl Trait reference#1212) (see instructions on rustc-dev-guide)
- Stabilization PR (Stabilize explicit_generic_args_with_impl_trait #96868) (see instructions on rustc-dev-guide)
Unresolved Questions
We should add a bit extra info when there are extra generic args andimpl Trait
(see Implement aexplicit_generic_args_with_impl_trait
feature gate #86176 (comment))
Implementation history
- Initital implementation in Implement a
explicit_generic_args_with_impl_trait
feature gate #86176
Background
Currently you cannot specify explicit generic arguments if a function uses impl Trait
anywhere its arguments. This is pretty annoying because it heavily restricts what kind of code you can write with functions that use impl Trait
compared to what is possible with explicit generics.
This difference between impl Trait
and explicit is in my opinion very unintuitive, it feels like you should be able to use them together. It is even mentioned as something in the original tracking issue for impl Trait
(#44721) that this would be desirable behaviour, but that there was a decision to hold off for the time being. That issue has been closed for long time, and there hasn't been any follow up as far as I can tell. So I'm opening up a new issue to discuss the feature and hopefully track adding it to the rust compiler.
Examples
#![feature(explicit_generic_args_with_impl_trait)]
fn find_from<I>(iter: impl IntoIterator<Item=impl Into<I>>, is_match: impl Fn(&I) -> bool) -> Option<I> {
for i in iter {
let i = i.into();
if (is_match)(&i) {
return Some(i)
}
}
None
}
fn main () {
matches!(find_from::<u32>(vec![2u8, 3, 4, 5,], |&x| x == 5), Some(5u32));
}