@@ -1633,6 +1633,80 @@ impl<T> [T] {
1633
1633
unsafe { ( from_raw_parts_mut ( ptr, mid) , from_raw_parts_mut ( ptr. add ( mid) , len - mid) ) }
1634
1634
}
1635
1635
1636
+ /// Divides one slice into an array and a remainder slice at an index.
1637
+ ///
1638
+ /// The array will contain all indices from `[0, N)` (excluding
1639
+ /// the index `N` itself) and the slice will contain all
1640
+ /// indices from `[N, len)` (excluding the index `len` itself).
1641
+ ///
1642
+ /// # Panics
1643
+ ///
1644
+ /// Panics if `N > len`.
1645
+ ///
1646
+ /// # Examples
1647
+ ///
1648
+ /// ```
1649
+ /// #![feature(slice_split_array)]
1650
+ ///
1651
+ /// let v = &[1, 2, 3, 4, 5, 6][..];
1652
+ ///
1653
+ /// {
1654
+ /// let (left, right) = v.split_array::<0>();
1655
+ /// assert_eq!(left, &[]);
1656
+ /// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1657
+ /// }
1658
+ ///
1659
+ /// {
1660
+ /// let (left, right) = v.split_array::<2>();
1661
+ /// assert_eq!(left, &[1, 2]);
1662
+ /// assert_eq!(right, [3, 4, 5, 6]);
1663
+ /// }
1664
+ ///
1665
+ /// {
1666
+ /// let (left, right) = v.split_array::<6>();
1667
+ /// assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
1668
+ /// assert_eq!(right, []);
1669
+ /// }
1670
+ /// ```
1671
+ #[ unstable( feature = "slice_split_array" , reason = "new API" , issue = "74674" ) ]
1672
+ #[ inline]
1673
+ pub fn split_array < const N : usize > ( & self ) -> ( & [ T ; N ] , & [ T ] ) {
1674
+ let ( a, b) = self . split_at ( N ) ;
1675
+ // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
1676
+ unsafe { ( & * ( a. as_ptr ( ) as * const [ T ; N ] ) , b) }
1677
+ }
1678
+
1679
+ /// Divides one mutable slice into an array and a remainder slice at an index.
1680
+ ///
1681
+ /// The array will contain all indices from `[0, N)` (excluding
1682
+ /// the index `N` itself) and the slice will contain all
1683
+ /// indices from `[N, len)` (excluding the index `len` itself).
1684
+ ///
1685
+ /// # Panics
1686
+ ///
1687
+ /// Panics if `N > len`.
1688
+ ///
1689
+ /// # Examples
1690
+ ///
1691
+ /// ```
1692
+ /// #![feature(slice_split_array)]
1693
+ ///
1694
+ /// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
1695
+ /// let (left, right) = v.split_array_mut::<2>();
1696
+ /// assert_eq!(left, &mut [1, 0]);
1697
+ /// assert_eq!(right, [3, 0, 5, 6]);
1698
+ /// left[1] = 2;
1699
+ /// right[1] = 4;
1700
+ /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
1701
+ /// ```
1702
+ #[ unstable( feature = "slice_split_array" , reason = "new API" , issue = "74674" ) ]
1703
+ #[ inline]
1704
+ pub fn split_array_mut < const N : usize > ( & mut self ) -> ( & mut [ T ; N ] , & mut [ T ] ) {
1705
+ let ( a, b) = self . split_at_mut ( N ) ;
1706
+ // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
1707
+ unsafe { ( & mut * ( a. as_mut_ptr ( ) as * mut [ T ; N ] ) , b) }
1708
+ }
1709
+
1636
1710
/// Returns an iterator over subslices separated by elements that match
1637
1711
/// `pred`. The matched element is not contained in the subslices.
1638
1712
///
0 commit comments