2 ms·
I'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 o
by throwaway894345 6y ago
I'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