5 ms·
‘Before [const generic], Rust had no native ability to deal with arrays bigger than 32 elements’. Is this a correct statement? I have seen posts talking about
by throwaway17_17 4y ago
‘Before [const generic], Rust had no native ability to deal with arrays bigger than 32 elements’.
Is this a correct statement? I have seen posts talking about const generics being a new thing as of 2022. Did Rust actually lack the ability to have an array with more than 32 elements? I find it hard to believe that there was no way to have an array of longer length and Rust still being a production level language.
- Animats 4y agoThere were some awful hacks to make integer parameters to generics sort of work before "const generic" went in. There were tables of named values for 0..32, then useful numbers such as 64, 128, 256, etc. Those haven't all been cleaned out yet.
- carry_bit 4y agoYou could have bigger arrays, what was missing were the trait implementations. Originally the traits were implemented using a macro, and the macro only generated implementations for up to length 32.
- masklinn 4y agoIt’s not quite correct no. Before const generics most traits were only implemented up to 32 elements though, which could be quite annoying. Even more so as the compilation error was not exactly informative.
- est31 4y agoYou have always been allowed to have arrays longer than 32 elements, but dealing with them used to be hard. Beyond the Copy trait, which is a compiler builtin, many traits weren't implemented for arrays with more than 32 elements. The first such change was implemented in 1.47.0 in 2020 where a bunch of traits were made work on all array sizes: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1470-2020-10-08 https://github.com/rust-lang/rust/blob/master/RELEASES.md#ve... It took a few releases, until 1.51.0 in 2021, until custom traits could be implemented for arrays of any length: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1510-2021-03-25 https://github.com/rust-lang/rust/blob/master/RELEASES.md#ve... And the feature is still limited. For example, legacy users like serde still can't switch to the new const generics based approach, because of the same issue that the Default trait is facing. Both traits could be using const generics, if they were allowed to break their API, but neither want to, so they are waiting for improvements that allow them to switch without doing a hard API break.
- jhugo 4y agoSince nobody else mentioned it, it's worth pointing out that what e.g. JS calls an array is Vec in Rust and can be as long as you want, with no ergonomic difference regardless of the length. Array in Rust specifically refers to an array whose length is known at compile time, i.e. a bunch of values concatenated on the stack, and that's what the limitations applied to. The quoted statement pissed me off a bit (I otherwise enjoyed the article) because it seems intended to mislead. The author should have known the colloquial meaning of "array", and "no ability to deal with" is factually incorrect.
- xobs 4y agoThis is still a problem with the latest version of Rust: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3da1b87b04c15d28ee71b1fc53dfb477 https://play.rust-lang.org/?version=stable&mode=debug&editio... If you set the array size to 32, then it works. You can get around this by using a macro, instead of `Default`, or implementing Default yourself, but it's still a limitation where you can't use an array of more than 32 elements.
- steveklabnik 4y agoIIRC this is a problem specific to Default, see here: https://github.com/rust-lang/rust/issues/61415#issuecomment-671081515 https://github.com/rust-lang/rust/issues/61415#issuecomment-... And here: https://github.com/rust-lang/rust/pull/62435 https://github.com/rust-lang/rust/pull/62435
- bacon_waffle 4y ago> You can get around this by using a macro, instead of `Default`, or implementing Default yourself For folks who aren't familiar with Rust, implementing Default yourself looks something like: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bbfc93b17a0657e83e760d0cf20e519b https://play.rust-lang.org/?version=stable&mode=debug&editio...
- dhosek 4y agoBut see other comments—it wasn’t an array limitation but rather a trait limitation.