]> git.proxmox.com Git - rustc.git/blame - src/doc/unstable-book/src/language-features/explicit-generic-args-with-impl-trait.md
New upstream version 1.62.1+dfsg1
[rustc.git] / src / doc / unstable-book / src / language-features / explicit-generic-args-with-impl-trait.md
CommitLineData
94222f64
XL
1# `explicit_generic_args_with_impl_trait`
2
3The tracking issue for this feature is: [#83701]
4
5[#83701]: https://github.com/rust-lang/rust/issues/83701
6
7------------------------
8
9The `explicit_generic_args_with_impl_trait` feature gate lets you specify generic arguments even
10when `impl Trait` is used in argument position.
11
12A simple example is:
13
14```rust
15#![feature(explicit_generic_args_with_impl_trait)]
16
17fn foo<T: ?Sized>(_f: impl AsRef<T>) {}
18
19fn main() {
20 foo::<str>("".to_string());
21}
22```
23
24This is currently rejected:
25
26```text
27error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
28 --> src/main.rs:6:11
29 |
306 | foo::<str>("".to_string());
31 | ^^^ explicit generic argument not allowed
32
33```
34
35However it would compile if `explicit_generic_args_with_impl_trait` is enabled.
36
37Note that the synthetic type parameters from `impl Trait` are still implicit and you
38cannot explicitly specify these:
39
40```rust,compile_fail
41#![feature(explicit_generic_args_with_impl_trait)]
42
43fn foo<T: ?Sized>(_f: impl AsRef<T>) {}
44fn bar<T: ?Sized, F: AsRef<T>>(_f: F) {}
45
46fn 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```