Closed
Description
Given the following code:
fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
[0; N]
}
fn main() {
let f = foo("foo");
}
The current output is:
error[E0282]: type annotations needed for `[usize; _]`
--> src/main.rs:6:13
|
6 | let f = foo("foo");
| - ^^^ cannot infer the value of const parameter `N` declared on the function `foo`
| |
| consider giving `f` the explicit type `[usize; _]`, where the type parameter `N` is specified
|
help: consider specifying the const argument
|
6 | let f = foo::<N>("foo");
| ^^^^^^^^
The first half is correct, but the problem is that final suggestion. If you try it:
fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {
[0; N]
}
fn main() {
let f = foo::<2>("foo");
}
you get another error because the suggestion can't work because of the impl std::fmt::Display
:
error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
--> src/main.rs:6:19
|
6 | let f = foo::<2>("foo");
| ^ explicit generic argument not allowed