@@ -1336,7 +1336,7 @@ impl<T> [T] {
13361336 assert_unsafe_precondition ! (
13371337 check_language_ub,
13381338 "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks" ,
1339- ( n: usize = N , len: usize = self . len( ) ) => n != 0 && len % n == 0 ,
1339+ ( n: usize = N , len: usize = self . len( ) ) => n != 0 && len. is_multiple_of ( n ) ,
13401340 ) ;
13411341 // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
13421342 let new_len = unsafe { exact_div ( self . len ( ) , N ) } ;
@@ -1532,7 +1532,7 @@ impl<T> [T] {
15321532 assert_unsafe_precondition ! (
15331533 check_language_ub,
15341534 "slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks" ,
1535- ( n: usize = N , len: usize = self . len( ) ) => n != 0 && len % n == 0
1535+ ( n: usize = N , len: usize = self . len( ) ) => n != 0 && len. is_multiple_of ( n )
15361536 ) ;
15371537 // SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
15381538 let new_len = unsafe { exact_div ( self . len ( ) , N ) } ;
@@ -2800,6 +2800,89 @@ impl<T> [T] {
28002800 None
28012801 }
28022802
2803+ /// Returns a subslice with the optional prefix removed.
2804+ ///
2805+ /// If the slice starts with `prefix`, returns the subslice after the prefix. If `prefix`
2806+ /// is empty or the slice does not start with `prefix`, simply returns the original slice.
2807+ /// If `prefix` is equal to the original slice, returns an empty slice.
2808+ ///
2809+ /// # Examples
2810+ ///
2811+ /// ```
2812+ /// #![feature(trim_prefix_suffix)]
2813+ ///
2814+ /// let v = &[10, 40, 30];
2815+ ///
2816+ /// // Prefix present - removes it
2817+ /// assert_eq!(v.trim_prefix(&[10]), &[40, 30][..]);
2818+ /// assert_eq!(v.trim_prefix(&[10, 40]), &[30][..]);
2819+ /// assert_eq!(v.trim_prefix(&[10, 40, 30]), &[][..]);
2820+ ///
2821+ /// // Prefix absent - returns original slice
2822+ /// assert_eq!(v.trim_prefix(&[50]), &[10, 40, 30][..]);
2823+ /// assert_eq!(v.trim_prefix(&[10, 50]), &[10, 40, 30][..]);
2824+ ///
2825+ /// let prefix : &str = "he";
2826+ /// assert_eq!(b"hello".trim_prefix(prefix.as_bytes()), b"llo".as_ref());
2827+ /// ```
2828+ #[ must_use = "returns the subslice without modifying the original" ]
2829+ #[ unstable( feature = "trim_prefix_suffix" , issue = "142312" ) ]
2830+ pub fn trim_prefix < P : SlicePattern < Item = T > + ?Sized > ( & self , prefix : & P ) -> & [ T ]
2831+ where
2832+ T : PartialEq ,
2833+ {
2834+ // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2835+ let prefix = prefix. as_slice ( ) ;
2836+ let n = prefix. len ( ) ;
2837+ if n <= self . len ( ) {
2838+ let ( head, tail) = self . split_at ( n) ;
2839+ if head == prefix {
2840+ return tail;
2841+ }
2842+ }
2843+ self
2844+ }
2845+
2846+ /// Returns a subslice with the optional suffix removed.
2847+ ///
2848+ /// If the slice ends with `suffix`, returns the subslice before the suffix. If `suffix`
2849+ /// is empty or the slice does not end with `suffix`, simply returns the original slice.
2850+ /// If `suffix` is equal to the original slice, returns an empty slice.
2851+ ///
2852+ /// # Examples
2853+ ///
2854+ /// ```
2855+ /// #![feature(trim_prefix_suffix)]
2856+ ///
2857+ /// let v = &[10, 40, 30];
2858+ ///
2859+ /// // Suffix present - removes it
2860+ /// assert_eq!(v.trim_suffix(&[30]), &[10, 40][..]);
2861+ /// assert_eq!(v.trim_suffix(&[40, 30]), &[10][..]);
2862+ /// assert_eq!(v.trim_suffix(&[10, 40, 30]), &[][..]);
2863+ ///
2864+ /// // Suffix absent - returns original slice
2865+ /// assert_eq!(v.trim_suffix(&[50]), &[10, 40, 30][..]);
2866+ /// assert_eq!(v.trim_suffix(&[50, 30]), &[10, 40, 30][..]);
2867+ /// ```
2868+ #[ must_use = "returns the subslice without modifying the original" ]
2869+ #[ unstable( feature = "trim_prefix_suffix" , issue = "142312" ) ]
2870+ pub fn trim_suffix < P : SlicePattern < Item = T > + ?Sized > ( & self , suffix : & P ) -> & [ T ]
2871+ where
2872+ T : PartialEq ,
2873+ {
2874+ // This function will need rewriting if and when SlicePattern becomes more sophisticated.
2875+ let suffix = suffix. as_slice ( ) ;
2876+ let ( len, n) = ( self . len ( ) , suffix. len ( ) ) ;
2877+ if n <= len {
2878+ let ( head, tail) = self . split_at ( len - n) ;
2879+ if tail == suffix {
2880+ return head;
2881+ }
2882+ }
2883+ self
2884+ }
2885+
28032886 /// Binary searches this slice for a given element.
28042887 /// If the slice is not sorted, the returned result is unspecified and
28052888 /// meaningless.
@@ -4887,7 +4970,7 @@ impl<T> [T] {
48874970
48884971 let byte_offset = elem_start. wrapping_sub ( self_start) ;
48894972
4890- if byte_offset % size_of :: < T > ( ) != 0 {
4973+ if ! byte_offset. is_multiple_of ( size_of :: < T > ( ) ) {
48914974 return None ;
48924975 }
48934976
@@ -4941,7 +5024,7 @@ impl<T> [T] {
49415024
49425025 let byte_start = subslice_start. wrapping_sub ( self_start) ;
49435026
4944- if byte_start % size_of :: < T > ( ) != 0 {
5027+ if ! byte_start. is_multiple_of ( size_of :: < T > ( ) ) {
49455028 return None ;
49465029 }
49475030
@@ -5179,15 +5262,17 @@ where
51795262}
51805263
51815264#[ stable( feature = "rust1" , since = "1.0.0" ) ]
5182- impl < T > Default for & [ T ] {
5265+ #[ rustc_const_unstable( feature = "const_default" , issue = "67792" ) ]
5266+ impl < T > const Default for & [ T ] {
51835267 /// Creates an empty slice.
51845268 fn default ( ) -> Self {
51855269 & [ ]
51865270 }
51875271}
51885272
51895273#[ stable( feature = "mut_slice_default" , since = "1.5.0" ) ]
5190- impl < T > Default for & mut [ T ] {
5274+ #[ rustc_const_unstable( feature = "const_default" , issue = "67792" ) ]
5275+ impl < T > const Default for & mut [ T ] {
51915276 /// Creates a mutable empty slice.
51925277 fn default ( ) -> Self {
51935278 & mut [ ]
0 commit comments