"pure" const generics defaults

I'm using the new feature " Const generics defaults", and I found that I don't know how to create "pure" Const generics defaults, for example:

struct Foo< const C: bool = false> {


fn main() {
    let foo = Foo {};

the compiliation will fail with error:

error[E0282]: type annotations needed for `Foo<C>`
 --> src/main.rs:9:15
9 |     let foo = Foo {};
  |         ---   ^^^^^^ cannot infer type for struct `Foo<{_: bool}>`
  |         |
  |         consider giving `foo` a type

It seems that a struct with const generics defaults need at least one normal generic parameter, I tried Foo::<> {} and Foo::<_> {} , but nothing works.

So do I have to pass the default value like this Foo::<false> {} even though false is the default, which leaves const generics defaults useless in this case?

The defaults only apply in types, not for struct constructors. So e.g. this works:

struct Foo<const C: bool = false> {


fn main() {
    let foo: Foo = Foo {};
1 Like

I must say, that is quite confusing. I looked at the two pieces of code for a good minute until I spotted the difference. I'm not really up to date what the const generics defaults discussions where about, but I guess there is some reason for this?

I got an extra question: if the type changes to

struct Foo<const C: bool = false, const D: bool = false> {}

and I only want to set D, and leave the C as default, how do I write?

I tried this:

fn main() {
    let foo: Foo<D: true> = Foo {};

But it doesn't work.

Check this

I believe there is no way (though I'm not quite 100% certain).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.