@@ -103,7 +103,7 @@ impl<T> [T] {
103
103
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
104
104
#[ inline]
105
105
pub fn first ( & self ) -> Option < & T > {
106
- self . get ( 0 )
106
+ if let [ first , .. ] = self { Some ( first ) } else { None }
107
107
}
108
108
109
109
/// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
@@ -121,7 +121,7 @@ impl<T> [T] {
121
121
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
122
122
#[ inline]
123
123
pub fn first_mut ( & mut self ) -> Option < & mut T > {
124
- self . get_mut ( 0 )
124
+ if let [ first , .. ] = self { Some ( first ) } else { None }
125
125
}
126
126
127
127
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -139,7 +139,7 @@ impl<T> [T] {
139
139
#[ stable( feature = "slice_splits" , since = "1.5.0" ) ]
140
140
#[ inline]
141
141
pub fn split_first ( & self ) -> Option < ( & T , & [ T ] ) > {
142
- if self . is_empty ( ) { None } else { Some ( ( & self [ 0 ] , & self [ 1 .. ] ) ) }
142
+ if let [ first , tail @ .. ] = self { Some ( ( first , tail ) ) } else { None }
143
143
}
144
144
145
145
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -159,12 +159,7 @@ impl<T> [T] {
159
159
#[ stable( feature = "slice_splits" , since = "1.5.0" ) ]
160
160
#[ inline]
161
161
pub fn split_first_mut ( & mut self ) -> Option < ( & mut T , & mut [ T ] ) > {
162
- if self . is_empty ( ) {
163
- None
164
- } else {
165
- let split = self . split_at_mut ( 1 ) ;
166
- Some ( ( & mut split. 0 [ 0 ] , split. 1 ) )
167
- }
162
+ if let [ first, tail @ ..] = self { Some ( ( first, tail) ) } else { None }
168
163
}
169
164
170
165
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -182,8 +177,7 @@ impl<T> [T] {
182
177
#[ stable( feature = "slice_splits" , since = "1.5.0" ) ]
183
178
#[ inline]
184
179
pub fn split_last ( & self ) -> Option < ( & T , & [ T ] ) > {
185
- let len = self . len ( ) ;
186
- if len == 0 { None } else { Some ( ( & self [ len - 1 ] , & self [ ..( len - 1 ) ] ) ) }
180
+ if let [ init @ .., last] = self { Some ( ( last, init) ) } else { None }
187
181
}
188
182
189
183
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -203,13 +197,7 @@ impl<T> [T] {
203
197
#[ stable( feature = "slice_splits" , since = "1.5.0" ) ]
204
198
#[ inline]
205
199
pub fn split_last_mut ( & mut self ) -> Option < ( & mut T , & mut [ T ] ) > {
206
- let len = self . len ( ) ;
207
- if len == 0 {
208
- None
209
- } else {
210
- let split = self . split_at_mut ( len - 1 ) ;
211
- Some ( ( & mut split. 1 [ 0 ] , split. 0 ) )
212
- }
200
+ if let [ init @ .., last] = self { Some ( ( last, init) ) } else { None }
213
201
}
214
202
215
203
/// Returns the last element of the slice, or `None` if it is empty.
@@ -226,8 +214,7 @@ impl<T> [T] {
226
214
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
227
215
#[ inline]
228
216
pub fn last ( & self ) -> Option < & T > {
229
- let last_idx = self . len ( ) . checked_sub ( 1 ) ?;
230
- self . get ( last_idx)
217
+ if let [ .., last] = self { Some ( last) } else { None }
231
218
}
232
219
233
220
/// Returns a mutable pointer to the last item in the slice.
@@ -245,8 +232,7 @@ impl<T> [T] {
245
232
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
246
233
#[ inline]
247
234
pub fn last_mut ( & mut self ) -> Option < & mut T > {
248
- let last_idx = self . len ( ) . checked_sub ( 1 ) ?;
249
- self . get_mut ( last_idx)
235
+ if let [ .., last] = self { Some ( last) } else { None }
250
236
}
251
237
252
238
/// Returns a reference to an element or subslice depending on the type of
0 commit comments