Change:
#[stable(feature = "chunks_exact", since = "1.31.0")] #[inline] pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> { <del> assert!(chunk_size != 0); </del> <ins> assert_ne!(chunk_size, 0); </ins> let rem = self.len() % chunk_size; let len = self.len() - rem; let (fst, snd) = self.split_at_mut(len); ChunksExactMut { v: fst, rem: snd, chunk_size } } <ins> /// Returns an iterator over `N` elements of the slice at a time, starting at the /// beginning of the slice. /// /// The chunks are slices and do not overlap. If `N` does not divide the length of the /// slice, then the last up to `N-1` elements will be omitted and can be retrieved /// from the `remainder` function of the iterator. /// /// This method is the const generic equivalent of [`chunks_exact`]. /// /// # Panics /// /// Panics if `N` is 0. This check will most probably get changed to a compile time /// error before this method gets stabilized. /// /// # Examples /// /// ``` /// #![feature(array_chunks)] /// let slice = ['l', 'o', 'r', 'e', 'm']; /// let mut iter = slice.array_chunks(); /// assert_eq!(iter.next().unwrap(), &['l', 'o']); /// assert_eq!(iter.next().unwrap(), &['r', 'e']); /// assert!(iter.next().is_none()); /// assert_eq!(iter.remainder(), &['m']); /// ``` /// /// [`chunks_exact`]: #method.chunks_exact #[unstable(feature = "array_chunks", issue = "74985")] #[inline] pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> { assert_ne!(N, 0); let len = self.len() / N; let (fst, snd) = self.split_at(len * N); // SAFETY: We cast a slice of `len * N` elements into // a slice of `len` many `N` elements chunks. let array_slice: &[[T; N]] = unsafe { from_raw_parts(fst.as_ptr().cast(), len) }; ArrayChunks { iter: array_slice.iter(), rem: snd } } </ins> /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end /// of the slice. ///

Discussion:
In the stdlib I'd like an iterator like chunksexact that yields fixed-sized arrays, using a const usize argument (mostly useful for short arrays). .chunksexactct::<3() .map( a + b + c) Those arrays could allow better optimizations and also better pattern matching. <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --This issue has been assigned to via . <!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOT_END --
This also applies to the method on slices.
We should actually be able to implement this and test it out unstably now.
Im willing to give this a shot in the weekend and next week!
assign
So, a simple PoC fails to compile horribly <details<summary<strongBacktrace</strong</summary<p</p</details
Notice that a freestanding function does work:
The more I go into this, the more obstacles I stumble upon. Below a list of all ICE's I encountered:
Keep going on, those bugs/limitations are worth fixing anyway :)
to_array() could be a new function related to take(), it collects the first N items of an iterator into an array of length given as const generic argument (it panics if the iterator isn't long enough). Usable like this (using iterate() from itertools):
EDIT: This is already PR'd:
We need a good built-in static_assert that works with constant generics too :)