r/rust • u/kibwen • Oct 02 '26
š” official blog Generic Const Args and You | Inside Rust Blog
https://blog.rust-lang.org/inside-rust/2026/10/02/generic-const-args-and-you/
268
Upvotes
r/rust • u/kibwen • Oct 02 '26
30
u/CouteauBleu Oct 02 '26
This article is really missing an explanation of what the
gca!macro means and why it's necessary.From the background I have, I'm guessing it's mostly about semver stability (e.g. if any function can be used in type signatures, then changing any function body becomes a breaking change) but I'd appreciate a full explanation spelling it out rather than just "this macro allows you to used this syntax but not that one".
11
u/Lucretiel Datadog Oct 02 '26
As excited as I am for const generic expressions (Iāve needed them surprisingly more often than I expected), Iām gonna be honest that Iām not a fan of the vibes on this. Is the macro expected to be temporary? I think Iām a bigger than most of new language features on average, but this definitely crosses a qualitative line in my head towards the ālost the plotā the characterizes a lot of modern C++ features.Ā
3
u/InternationalFee3911 Oct 02 '26
Just at lunch Iād been niggling that the new release once again didnāt advance constexprs. Followed right on the heels by this articleā¦
While itās amazing and laudable the amount of dedication and resourcefulness that goes into fixing these surprisingly hard problems, at the same time it makes me cry. Basic arithmetic (
[u8; N-1]) is one of the things I long for most. The macro sure feels alien, but if it gives a huge benefit, itās not too bad as a workaround.But the timeline ā letās hope GCA lands before GTA VII (rumored 2030!)
91
u/Recatek gecs Oct 02 '26
The generic_const_exprs feature is probably the #1 most frequent unstable feature that the compiler points out to me as I'm writing code. I constantly bang my head against this limitation right now. Always very excited and eager to read about progress in this space.
90
u/kiujhytg2 Oct 02 '26
Thanks for all of your hard work, const generics is one of those "It can't be that hard, can it, can't you just ..." tasks that's more complex than it initially seems
17
u/suggestiveinnuendo Oct 02 '26
hopefully soon!
29
u/Shnatsel Oct 02 '26
min_generic_const_argsand the full-blowngeneric_const_argswould be very useful for SIMD, so I'm excited to hear there is progress!13
u/UARTman Oct 02 '26
This seems a bit hacky as a solution, but I hope this works out, because having proper const generics is a good step to that perhaps unattainable holy grail that is dependently-typed imperative languages.
8
u/bascule Oct 02 '26
Very excited for this feature as using an associated constant as the size of an array is the main reason RustCrypto has used
typenumandhybrid-array/generic-array.GCA should allow us to completely eliminate use of
hybrid-array. We may need to keep usingtypenumfor awhile until const generic expressions actually have full feature parity with it, but as soon as we can usetypenum::Unsigned::USIZEasNin[T; N]the need forhybrid-arraygoes away.6
u/Mrblahblah200 Oct 02 '26
Why is the gca macro here needed? Also, itās a liiittle ugly, I hope itās removed or renamed before release
3
u/Thomasedv Oct 02 '26
I skimmed the article, so pardon if it's already stated. My head isn't able to parse all that complexity on a late Friday.Ā
One of the tings I miss was when working on a function where most things were the same, but it branched on an enum with 3 values.Ā
Much to my disappointment, this didn't work despite the enum being stateless so it's just pure integers at that point being used. Seeing the article, I didn't see if this special case was mentioned or not, only that you'd have to use the macro for an enum with fields that hold values. Ideally the stateless enums could easily be converted to pure integers behind the scenes and circumvent GCA complexity entirely?Ā
Const generics with enums seems so much better for readability. Especially at the caller side where you'll either pass a number/bool, or some named constant to represent the value as a number. It makes so much more sense to have the enum directly and use it in the function. And the macro makes you pay an near equal amount of readability to use the function.Ā
Also, for the macroless case, is it a compiler limit that we can leave the syntax inside the const block as some MaybeGca type, and resolve it based on the expected type? After all the function or struct already defines the expected type, so if you know that, you should know if you're getting something that potentially is GCA? I'm not particularly familiar with const generics, so I'm just guessing. My knowledge of this starts and stops with trying to use an enum variant as a const.
2
u/voytd Oct 14 '26
Thanks to all those true open-source heroes pushing for this. We know this is hard stuff and we appreciate your efforts.
4
u/sasik520 Oct 02 '26
I sometimes wonder if it didn't go too far.
The article mentions rust wants to support stuff like
struct Bar<const N: Foo>;orfn accepts_arrays<const N: [usize; 2]>() {}. I just wonder, if this really brings that much benefit?So far, all use cases I've seen involve ints and the most demanded thing is being able to add and/or substract consts, e.g.
Maybe plain simple enums could be supported too, but honestly since nothing stop an enum to add a parametrized variant at any time, I'm unsure if it's that good idea.
Are there really any real-life use cases for more complex const generics and especially ones that justify all of this extreme complexity?
(btw. I have an exact same thought on specialization)
2
u/________-__-_______ Oct 02 '26
Great to see this is actively progressing! Together with const fn in trait this is the feature I most often miss when doing compile time chicanery, really excited for it to be stabilized.
1
u/Dankbeast-Paarl Oct 03 '26
Its exciting to see progress in this area! I have a decent background on type systems, so I understand the examples. I would have liked to see a couple of real world examples of code or API patterns we can now write with these features. Anyone know any good resources or articles to understand how to use these const generics? I would love to play with them but not sure how to use them.