Reputation: 6170
I'm providing a default value for a const generic type but the Rust compiler tells me it "cannot infer the value of const parameter". It seems to ignore the default. Am I using this feature wrong? Is this how it is supposed to work? Then why use defaults at all? I use nightly 1.60.
const DEFAULT_N: usize = 73;
struct Foo<const N: usize = DEFAULT_N>;
impl<const N: usize> Foo<N> {
fn new() -> Self {
println!("N is: {}", N);
Self
}
}
fn main() {
Foo::new();
}
Upvotes: 6
Views: 1578
Reputation: 2247
I got it working like this
const DEFAULT_N: usize = 73;
struct Foo<const N: usize = DEFAULT_N>;
impl<const N: usize> Foo<N> {
fn new() -> Self {
println!("N is: {}", N);
Self
}
}
fn main() {
let f:Foo = Foo::new(); // just added the asked type annotation here
}
Upvotes: 4