@@ -1336,7 +1336,7 @@ impl<T> [T] {
1336
1336
assert_unsafe_precondition ! (
1337
1337
check_language_ub,
1338
1338
"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 ) ,
1340
1340
) ;
1341
1341
// SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1342
1342
let new_len = unsafe { exact_div ( self . len ( ) , N ) } ;
@@ -1532,7 +1532,7 @@ impl<T> [T] {
1532
1532
assert_unsafe_precondition ! (
1533
1533
check_language_ub,
1534
1534
"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 )
1536
1536
) ;
1537
1537
// SAFETY: Caller must guarantee that `N` is nonzero and exactly divides the slice length
1538
1538
let new_len = unsafe { exact_div ( self . len ( ) , N ) } ;
@@ -2800,6 +2800,89 @@ impl<T> [T] {
2800
2800
None
2801
2801
}
2802
2802
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
+
2803
2886
/// Binary searches this slice for a given element.
2804
2887
/// If the slice is not sorted, the returned result is unspecified and
2805
2888
/// meaningless.
@@ -4887,7 +4970,7 @@ impl<T> [T] {
4887
4970
4888
4971
let byte_offset = elem_start. wrapping_sub ( self_start) ;
4889
4972
4890
- if byte_offset % size_of :: < T > ( ) != 0 {
4973
+ if ! byte_offset. is_multiple_of ( size_of :: < T > ( ) ) {
4891
4974
return None ;
4892
4975
}
4893
4976
@@ -4941,7 +5024,7 @@ impl<T> [T] {
4941
5024
4942
5025
let byte_start = subslice_start. wrapping_sub ( self_start) ;
4943
5026
4944
- if byte_start % size_of :: < T > ( ) != 0 {
5027
+ if ! byte_start. is_multiple_of ( size_of :: < T > ( ) ) {
4945
5028
return None ;
4946
5029
}
4947
5030
@@ -5179,15 +5262,17 @@ where
5179
5262
}
5180
5263
5181
5264
#[ 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 ] {
5183
5267
/// Creates an empty slice.
5184
5268
fn default ( ) -> Self {
5185
5269
& [ ]
5186
5270
}
5187
5271
}
5188
5272
5189
5273
#[ 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 ] {
5191
5276
/// Creates a mutable empty slice.
5192
5277
fn default ( ) -> Self {
5193
5278
& mut [ ]
0 commit comments