6 ms·
> For a long time, even the standard library methods for arrays were limited to arrays of length at most 32 due to this problem. This restriction was finally li
by wyldfire 6y ago
> For a long time, even the standard library methods for arrays were limited to arrays of length at most 32 due to this problem. This restriction was finally lifted in Rust 1.47 - a change that was made possible by const generics.
I haven't been following every single update, so it's good to know that this constraint is no longer present (and has been gone for a bit).
- ibraheemdev 6y agoIt was removed from that standard library because std is built on nightly (which had const generics a while ago). Now that const generics are stable, crates can take advantage of this too. Previously they were using macros to generate the 32 array implementations.
- adkadskhj 6y agoI was bit by this yesterday. I had a 32 array, but the implementations only went up to 31 (with 0, so 32 in total). Found that quite amusing. 1 day away, 1 array element away. 32 seems so normal that it made me question why the default was 33, to include 0-32, but whatever heh. Glad to see this!
- littlestymaar 6y ago> It was removed from that standard library because std is built on nightly IIRC std and the compiler are built with the previous stable version of the compiler but with some special flag that allow using unstable features for bootstrapping.
- Lev1a 6y agoIIRC it goes further than that. Previous stable (0) compiles current source (1) into a temporary new stable (2), then (2) is used to compile (1) into the actual release version (3). Could be wrong though, it has been a long time since I stumbled over a description of the release process.
- tgb 6y agoWhat are you quoting? I can't find that in the article linked here and I don't understand what it's saying. Surely Rust didn't cap arrays at length 32?
- hctaw 6y agoMethods for arrays were capped at 32. So for example #[derive(Copy, Clone, Debug)] struct S { a: [i32; 33], } Would not compile, since Copy, Clone, and Debug were not implemented for arrays larger than 32.
- est31 6y agoNit: Copy has always been a builtin auto trait so it has always been implemented. You are right for the other traits. (also as other commenters point out, std builtin types like Clone etc have already been supporting larger array sizes because of the standard library's ability to use unstable features, but before const generics were available as an unstable feature, that restriction very much did exist, and now crates from the ecosystem can use it too).
- wyldfire 6y ago> If you'd like to learn more about const generics you can also check out the "Const Generics MVP Hits Beta" blog post for more information about the feature and its current restrictions. The article links to more details in that post [1], that's what the citation was from, sorry. I thought it was conspicuous that they had '32' in the example because I remembered it as a limitation before. No, arrays were not limited in length but IIRC generic types weren't able to make an array size generic across arbitrary counts. [1] https://blog.rust-lang.org/2021/02/26/const-generics-mvp-beta.html https://blog.rust-lang.org/2021/02/26/const-generics-mvp-bet...
- anchpop 6y agoArrays could be any length, but generic functions were only generic for arrays of length 32 or lower.
- exdsq 6y ago
- throwaway894345 6y agoI'm a bit of a noob, but could const generics allow for a const generic `.windows<const WIN_SIZE: usize>()` function that returns a fixed length array instead of a slice? E.g., instead of: foo.windows(2).map(|window| match window { [l, r] => ..., _ => panic!(...), })... I would like to write: foo.windows::<2>().map(|[l, r]| ...)...
- ogoffart 6y agoYes: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.array_windows https://doc.rust-lang.org/stable/std/primitive.slice.html#me...
- throwaway894345 6y agoPerfect, that's exactly what I want!
- adrian17 6y agoNote that you don't even need to supply the size, as it'll get deduced: for &[l, r] in data.array_windows() {
- gpm 6y agoYes, but note that if you do that the parameter has to be constant, you can't do foo.windows::<runtime_expr>()
- throwaway894345 6y agoYeah, that's fine. I'm making a static site generator and I want each post to have optional links to the next and previous posts, so something like: let pages = posts.windows::<3>().map(|[prev, this, next]| Page { item: this, prev: Some(to_url(prev)), next: Some(to_url(next)), }); // then attach first and last pages