]>
Commit | Line | Data |
---|---|---|
94222f64 XL |
1 | # `explicit_generic_args_with_impl_trait` |
2 | ||
3 | The tracking issue for this feature is: [#83701] | |
4 | ||
5 | [#83701]: https://github.com/rust-lang/rust/issues/83701 | |
6 | ||
7 | ------------------------ | |
8 | ||
9 | The `explicit_generic_args_with_impl_trait` feature gate lets you specify generic arguments even | |
10 | when `impl Trait` is used in argument position. | |
11 | ||
12 | A simple example is: | |
13 | ||
14 | ```rust | |
15 | #![feature(explicit_generic_args_with_impl_trait)] | |
16 | ||
17 | fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | |
18 | ||
19 | fn main() { | |
20 | foo::<str>("".to_string()); | |
21 | } | |
22 | ``` | |
23 | ||
24 | This is currently rejected: | |
25 | ||
26 | ```text | |
27 | error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position | |
28 | --> src/main.rs:6:11 | |
29 | | | |
30 | 6 | foo::<str>("".to_string()); | |
31 | | ^^^ explicit generic argument not allowed | |
32 | ||
33 | ``` | |
34 | ||
35 | However it would compile if `explicit_generic_args_with_impl_trait` is enabled. | |
36 | ||
37 | Note that the synthetic type parameters from `impl Trait` are still implicit and you | |
38 | cannot explicitly specify these: | |
39 | ||
40 | ```rust,compile_fail | |
41 | #![feature(explicit_generic_args_with_impl_trait)] | |
42 | ||
43 | fn foo<T: ?Sized>(_f: impl AsRef<T>) {} | |
44 | fn bar<T: ?Sized, F: AsRef<T>>(_f: F) {} | |
45 | ||
46 | fn main() { | |
47 | bar::<str, _>("".to_string()); // Okay | |
48 | bar::<str, String>("".to_string()); // Okay | |
49 | ||
50 | foo::<str>("".to_string()); // Okay | |
51 | foo::<str, String>("".to_string()); // Error, you cannot specify `impl Trait` explicitly | |
52 | } | |
53 | ``` |