@@ -882,6 +882,36 @@ impl<T> [T] {
882
882
ChunksExactMut :: new ( self , chunk_size)
883
883
}
884
884
885
+ /// Splits the slice into a slice of `N`-element arrays,
886
+ /// starting at the beginning of the slice,
887
+ /// and a remainder slice with length strictly less than `N`.
888
+ ///
889
+ /// # Panics
890
+ ///
891
+ /// Panics if `N` is 0. This check will most probably get changed to a compile time
892
+ /// error before this method gets stabilized.
893
+ ///
894
+ /// # Examples
895
+ ///
896
+ /// ```
897
+ /// #![feature(slice_as_chunks)]
898
+ /// let slice = ['l', 'o', 'r', 'e', 'm'];
899
+ /// let (chunks, remainder) = slice.as_chunks();
900
+ /// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]);
901
+ /// assert_eq!(remainder, &['m']);
902
+ /// ```
903
+ #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
904
+ #[ inline]
905
+ pub fn as_chunks < const N : usize > ( & self ) -> ( & [ [ T ; N ] ] , & [ T ] ) {
906
+ assert_ne ! ( N , 0 ) ;
907
+ let len = self . len ( ) / N ;
908
+ let ( multiple_of_n, remainder) = self . split_at ( len * N ) ;
909
+ // SAFETY: We cast a slice of `len * N` elements into
910
+ // a slice of `len` many `N` elements chunks.
911
+ let array_slice: & [ [ T ; N ] ] = unsafe { from_raw_parts ( multiple_of_n. as_ptr ( ) . cast ( ) , len) } ;
912
+ ( array_slice, remainder)
913
+ }
914
+
885
915
/// Returns an iterator over `N` elements of the slice at a time, starting at the
886
916
/// beginning of the slice.
887
917
///
@@ -916,6 +946,43 @@ impl<T> [T] {
916
946
ArrayChunks :: new ( self )
917
947
}
918
948
949
+ /// Splits the slice into a slice of `N`-element arrays,
950
+ /// starting at the beginning of the slice,
951
+ /// and a remainder slice with length strictly less than `N`.
952
+ ///
953
+ /// # Panics
954
+ ///
955
+ /// Panics if `N` is 0. This check will most probably get changed to a compile time
956
+ /// error before this method gets stabilized.
957
+ ///
958
+ /// # Examples
959
+ ///
960
+ /// ```
961
+ /// #![feature(slice_as_chunks)]
962
+ /// let v = &mut [0, 0, 0, 0, 0];
963
+ /// let mut count = 1;
964
+ ///
965
+ /// let (chunks, remainder) = v.as_chunks_mut();
966
+ /// remainder[0] = 9;
967
+ /// for chunk in chunks {
968
+ /// *chunk = [count; 2];
969
+ /// count += 1;
970
+ /// }
971
+ /// assert_eq!(v, &[1, 1, 2, 2, 9]);
972
+ /// ```
973
+ #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
974
+ #[ inline]
975
+ pub fn as_chunks_mut < const N : usize > ( & mut self ) -> ( & mut [ [ T ; N ] ] , & mut [ T ] ) {
976
+ assert_ne ! ( N , 0 ) ;
977
+ let len = self . len ( ) / N ;
978
+ let ( multiple_of_n, remainder) = self . split_at_mut ( len * N ) ;
979
+ let array_slice: & mut [ [ T ; N ] ] =
980
+ // SAFETY: We cast a slice of `len * N` elements into
981
+ // a slice of `len` many `N` elements chunks.
982
+ unsafe { from_raw_parts_mut ( multiple_of_n. as_mut_ptr ( ) . cast ( ) , len) } ;
983
+ ( array_slice, remainder)
984
+ }
985
+
919
986
/// Returns an iterator over `N` elements of the slice at a time, starting at the
920
987
/// beginning of the slice.
921
988
///
0 commit comments