@@ -1665,6 +1665,80 @@ impl<T> [T] {
1665
1665
unsafe { ( from_raw_parts_mut ( ptr, mid) , from_raw_parts_mut ( ptr. add ( mid) , len - mid) ) }
1666
1666
}
1667
1667
1668
+ /// Divides one slice into an array and a remainder slice at an index.
1669
+ ///
1670
+ /// The array will contain all indices from `[0, N)` (excluding
1671
+ /// the index `N` itself) and the slice will contain all
1672
+ /// indices from `[N, len)` (excluding the index `len` itself).
1673
+ ///
1674
+ /// # Panics
1675
+ ///
1676
+ /// Panics if `N > len`.
1677
+ ///
1678
+ /// # Examples
1679
+ ///
1680
+ /// ```
1681
+ /// #![feature(split_array)]
1682
+ ///
1683
+ /// let v = &[1, 2, 3, 4, 5, 6][..];
1684
+ ///
1685
+ /// {
1686
+ /// let (left, right) = v.split_array_ref::<0>();
1687
+ /// assert_eq!(left, &[]);
1688
+ /// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1689
+ /// }
1690
+ ///
1691
+ /// {
1692
+ /// let (left, right) = v.split_array_ref::<2>();
1693
+ /// assert_eq!(left, &[1, 2]);
1694
+ /// assert_eq!(right, [3, 4, 5, 6]);
1695
+ /// }
1696
+ ///
1697
+ /// {
1698
+ /// let (left, right) = v.split_array_ref::<6>();
1699
+ /// assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
1700
+ /// assert_eq!(right, []);
1701
+ /// }
1702
+ /// ```
1703
+ #[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1704
+ #[ inline]
1705
+ pub fn split_array_ref < const N : usize > ( & self ) -> ( & [ T ; N ] , & [ T ] ) {
1706
+ let ( a, b) = self . split_at ( N ) ;
1707
+ // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
1708
+ unsafe { ( & * ( a. as_ptr ( ) as * const [ T ; N ] ) , b) }
1709
+ }
1710
+
1711
+ /// Divides one mutable slice into an array and a remainder slice at an index.
1712
+ ///
1713
+ /// The array will contain all indices from `[0, N)` (excluding
1714
+ /// the index `N` itself) and the slice will contain all
1715
+ /// indices from `[N, len)` (excluding the index `len` itself).
1716
+ ///
1717
+ /// # Panics
1718
+ ///
1719
+ /// Panics if `N > len`.
1720
+ ///
1721
+ /// # Examples
1722
+ ///
1723
+ /// ```
1724
+ /// #![feature(split_array)]
1725
+ ///
1726
+ /// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
1727
+ /// let (left, right) = v.split_array_mut::<2>();
1728
+ /// assert_eq!(left, &mut [1, 0]);
1729
+ /// assert_eq!(right, [3, 0, 5, 6]);
1730
+ /// left[1] = 2;
1731
+ /// right[1] = 4;
1732
+ /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
1733
+ /// ```
1734
+ #[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1735
+ #[ inline]
1736
+ pub fn split_array_mut < const N : usize > ( & mut self ) -> ( & mut [ T ; N ] , & mut [ T ] ) {
1737
+ let ( a, b) = self . split_at_mut ( N ) ;
1738
+ // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
1739
+ unsafe { ( & mut * ( a. as_mut_ptr ( ) as * mut [ T ; N ] ) , b) }
1740
+ }
1741
+
1668
1742
/// Returns an iterator over subslices separated by elements that match
1669
1743
/// `pred`. The matched element is not contained in the subslices.
1670
1744
///
0 commit comments