@@ -1705,6 +1705,84 @@ impl<T> [T] {
17051705 unsafe { ( & mut * ( a. as_mut_ptr ( ) as * mut [ T ; N ] ) , b) }
17061706 }
17071707
1708+ /// Divides one slice into an array and a remainder slice at an index from
1709+ /// the end.
1710+ ///
1711+ /// The slice will contain all indices from `[0, len - N)` (excluding
1712+ /// the index `len - N` itself) and the array will contain all
1713+ /// indices from `[len - N, len)` (excluding the index `len` itself).
1714+ ///
1715+ /// # Panics
1716+ ///
1717+ /// Panics if `N > len`.
1718+ ///
1719+ /// # Examples
1720+ ///
1721+ /// ```
1722+ /// #![feature(split_array)]
1723+ ///
1724+ /// let v = &[1, 2, 3, 4, 5, 6][..];
1725+ ///
1726+ /// {
1727+ /// let (left, right) = v.rsplit_array_ref::<0>();
1728+ /// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
1729+ /// assert_eq!(right, &[]);
1730+ /// }
1731+ ///
1732+ /// {
1733+ /// let (left, right) = v.rsplit_array_ref::<2>();
1734+ /// assert_eq!(left, [1, 2, 3, 4]);
1735+ /// assert_eq!(right, &[5, 6]);
1736+ /// }
1737+ ///
1738+ /// {
1739+ /// let (left, right) = v.rsplit_array_ref::<6>();
1740+ /// assert_eq!(left, []);
1741+ /// assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
1742+ /// }
1743+ /// ```
1744+ #[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1745+ #[ inline]
1746+ pub fn rsplit_array_ref < const N : usize > ( & self ) -> ( & [ T ] , & [ T ; N ] ) {
1747+ assert ! ( N <= self . len( ) ) ;
1748+ let ( a, b) = self . split_at ( self . len ( ) - N ) ;
1749+ // SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at)
1750+ unsafe { ( a, & * ( b. as_ptr ( ) as * const [ T ; N ] ) ) }
1751+ }
1752+
1753+ /// Divides one mutable slice into an array and a remainder slice at an
1754+ /// index from the end.
1755+ ///
1756+ /// The slice will contain all indices from `[0, len - N)` (excluding
1757+ /// the index `N` itself) and the array will contain all
1758+ /// indices from `[len - N, len)` (excluding the index `len` itself).
1759+ ///
1760+ /// # Panics
1761+ ///
1762+ /// Panics if `N > len`.
1763+ ///
1764+ /// # Examples
1765+ ///
1766+ /// ```
1767+ /// #![feature(split_array)]
1768+ ///
1769+ /// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
1770+ /// let (left, right) = v.rsplit_array_mut::<4>();
1771+ /// assert_eq!(left, [1, 0]);
1772+ /// assert_eq!(right, &mut [3, 0, 5, 6]);
1773+ /// left[1] = 2;
1774+ /// right[1] = 4;
1775+ /// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
1776+ /// ```
1777+ #[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1778+ #[ inline]
1779+ pub fn rsplit_array_mut < const N : usize > ( & mut self ) -> ( & mut [ T ] , & mut [ T ; N ] ) {
1780+ assert ! ( N <= self . len( ) ) ;
1781+ let ( a, b) = self . split_at_mut ( self . len ( ) - N ) ;
1782+ // SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
1783+ unsafe { ( a, & mut * ( b. as_mut_ptr ( ) as * mut [ T ; N ] ) ) }
1784+ }
1785+
17081786 /// Returns an iterator over subslices separated by elements that match
17091787 /// `pred`. The matched element is not contained in the subslices.
17101788 ///
0 commit comments