@@ -119,7 +119,7 @@ impl<T> [T] {
119
119
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
120
120
#[ inline]
121
121
pub fn first ( & self ) -> Option < & T > {
122
- if self . is_empty ( ) { None } else { Some ( & self [ 0 ] ) }
122
+ self . get ( 0 )
123
123
}
124
124
125
125
/// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
@@ -137,7 +137,7 @@ impl<T> [T] {
137
137
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
138
138
#[ inline]
139
139
pub fn first_mut ( & mut self ) -> Option < & mut T > {
140
- if self . is_empty ( ) { None } else { Some ( & mut self [ 0 ] ) }
140
+ self . get_mut ( 0 )
141
141
}
142
142
143
143
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -239,7 +239,8 @@ impl<T> [T] {
239
239
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
240
240
#[ inline]
241
241
pub fn last ( & self ) -> Option < & T > {
242
- if self . is_empty ( ) { None } else { Some ( & self [ self . len ( ) - 1 ] ) }
242
+ let last_idx = self . len ( ) . checked_sub ( 1 ) ?;
243
+ self . get ( last_idx)
243
244
}
244
245
245
246
/// Returns a mutable pointer to the last item in the slice.
@@ -257,9 +258,8 @@ impl<T> [T] {
257
258
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
258
259
#[ inline]
259
260
pub fn last_mut ( & mut self ) -> Option < & mut T > {
260
- let len = self . len ( ) ;
261
- if len == 0 { return None ; }
262
- Some ( & mut self [ len - 1 ] )
261
+ let last_idx = self . len ( ) . checked_sub ( 1 ) ?;
262
+ self . get_mut ( last_idx)
263
263
}
264
264
265
265
/// Returns a reference to an element or subslice depending on the type of
@@ -1618,6 +1618,63 @@ impl<T> [T] {
1618
1618
}
1619
1619
}
1620
1620
1621
+ /// Copies elements from one part of the slice to another part of itself,
1622
+ /// using a memmove.
1623
+ ///
1624
+ /// `src` is the range within `self` to copy from. `dest` is the starting
1625
+ /// index of the range within `self` to copy to, which will have the same
1626
+ /// length as `src`. The two ranges may overlap. The ends of the two ranges
1627
+ /// must be less than or equal to `self.len()`.
1628
+ ///
1629
+ /// # Panics
1630
+ ///
1631
+ /// This function will panic if either range exceeds the end of the slice,
1632
+ /// or if the end of `src` is before the start.
1633
+ ///
1634
+ /// # Examples
1635
+ ///
1636
+ /// Copying four bytes within a slice:
1637
+ ///
1638
+ /// ```
1639
+ /// # #![feature(copy_within)]
1640
+ /// let mut bytes = *b"Hello, World!";
1641
+ ///
1642
+ /// bytes.copy_within(1..5, 8);
1643
+ ///
1644
+ /// assert_eq!(&bytes, b"Hello, Wello!");
1645
+ /// ```
1646
+ #[ unstable( feature = "copy_within" , issue = "54236" ) ]
1647
+ pub fn copy_within < R : ops:: RangeBounds < usize > > ( & mut self , src : R , dest : usize )
1648
+ where
1649
+ T : Copy ,
1650
+ {
1651
+ let src_start = match src. start_bound ( ) {
1652
+ ops:: Bound :: Included ( & n) => n,
1653
+ ops:: Bound :: Excluded ( & n) => n
1654
+ . checked_add ( 1 )
1655
+ . unwrap_or_else ( || slice_index_overflow_fail ( ) ) ,
1656
+ ops:: Bound :: Unbounded => 0 ,
1657
+ } ;
1658
+ let src_end = match src. end_bound ( ) {
1659
+ ops:: Bound :: Included ( & n) => n
1660
+ . checked_add ( 1 )
1661
+ . unwrap_or_else ( || slice_index_overflow_fail ( ) ) ,
1662
+ ops:: Bound :: Excluded ( & n) => n,
1663
+ ops:: Bound :: Unbounded => self . len ( ) ,
1664
+ } ;
1665
+ assert ! ( src_start <= src_end, "src end is before src start" ) ;
1666
+ assert ! ( src_end <= self . len( ) , "src is out of bounds" ) ;
1667
+ let count = src_end - src_start;
1668
+ assert ! ( dest <= self . len( ) - count, "dest is out of bounds" ) ;
1669
+ unsafe {
1670
+ ptr:: copy (
1671
+ self . get_unchecked ( src_start) ,
1672
+ self . get_unchecked_mut ( dest) ,
1673
+ count,
1674
+ ) ;
1675
+ }
1676
+ }
1677
+
1621
1678
/// Swaps all elements in `self` with those in `other`.
1622
1679
///
1623
1680
/// The length of `other` must be the same as `self`.
0 commit comments