@@ -3335,7 +3335,9 @@ impl<T> [T] {
3335
3335
#[ inline]
3336
3336
#[ unstable( feature = "slice_take" , issue = "62280" ) ]
3337
3337
pub fn take_first < ' a > ( self : & mut & ' a Self ) -> Option < & ' a T > {
3338
- self . take ( ..=0 ) . map ( |res| & res[ 0 ] )
3338
+ let ( first, rem) = self . split_first ( ) ?;
3339
+ * self = rem;
3340
+ Some ( first)
3339
3341
}
3340
3342
3341
3343
/// Returns a mutable reference to the first element of the slice,
@@ -3358,7 +3360,9 @@ impl<T> [T] {
3358
3360
#[ inline]
3359
3361
#[ unstable( feature = "slice_take" , issue = "62280" ) ]
3360
3362
pub fn take_first_mut < ' a > ( self : & mut & ' a mut Self ) -> Option < & ' a mut T > {
3361
- self . take_mut ( ..=0 ) . map ( |res| & mut res[ 0 ] )
3363
+ let ( first, rem) = mem:: take ( self ) . split_first_mut ( ) ?;
3364
+ * self = rem;
3365
+ Some ( first)
3362
3366
}
3363
3367
3364
3368
/// Returns a reference to the last element of the slice,
@@ -3380,8 +3384,9 @@ impl<T> [T] {
3380
3384
#[ inline]
3381
3385
#[ unstable( feature = "slice_take" , issue = "62280" ) ]
3382
3386
pub fn take_last < ' a > ( self : & mut & ' a Self ) -> Option < & ' a T > {
3383
- let i = self . len ( ) . checked_sub ( 1 ) ?;
3384
- self . take ( i..) . map ( |res| & res[ 0 ] )
3387
+ let ( last, rem) = self . split_last ( ) ?;
3388
+ * self = rem;
3389
+ Some ( last)
3385
3390
}
3386
3391
3387
3392
/// Returns a mutable reference to the last element of the slice,
@@ -3404,8 +3409,9 @@ impl<T> [T] {
3404
3409
#[ inline]
3405
3410
#[ unstable( feature = "slice_take" , issue = "62280" ) ]
3406
3411
pub fn take_last_mut < ' a > ( self : & mut & ' a mut Self ) -> Option < & ' a mut T > {
3407
- let i = self . len ( ) . checked_sub ( 1 ) ?;
3408
- self . take_mut ( i..) . map ( |res| & mut res[ 0 ] )
3412
+ let ( last, rem) = mem:: take ( self ) . split_last_mut ( ) ?;
3413
+ * self = rem;
3414
+ Some ( last)
3409
3415
}
3410
3416
}
3411
3417
0 commit comments