@@ -288,10 +288,12 @@ impl<T> [T] {
288
288
/// Returns a reference to an element or subslice, without doing bounds
289
289
/// checking.
290
290
///
291
- /// This is generally not recommended, use with caution!
291
+ /// For a safe alternative see [`get`].
292
+ ///
293
+ /// # Safety
294
+ ///
292
295
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
293
296
/// even if the resulting reference is not used.
294
- /// For a safe alternative see [`get`].
295
297
///
296
298
/// [`get`]: #method.get
297
299
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
@@ -320,10 +322,12 @@ impl<T> [T] {
320
322
/// Returns a mutable reference to an element or subslice, without doing
321
323
/// bounds checking.
322
324
///
323
- /// This is generally not recommended, use with caution!
325
+ /// For a safe alternative see [`get_mut`].
326
+ ///
327
+ /// # Safety
328
+ ///
324
329
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
325
330
/// even if the resulting reference is not used.
326
- /// For a safe alternative see [`get_mut`].
327
331
///
328
332
/// [`get_mut`]: #method.get_mut
329
333
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
@@ -1133,20 +1137,20 @@ impl<T> [T] {
1133
1137
///
1134
1138
/// {
1135
1139
/// let (left, right) = v.split_at(0);
1136
- /// assert !(left == []);
1137
- /// assert !(right == [1, 2, 3, 4, 5, 6]);
1140
+ /// assert_eq !(left, []);
1141
+ /// assert_eq !(right, [1, 2, 3, 4, 5, 6]);
1138
1142
/// }
1139
1143
///
1140
1144
/// {
1141
1145
/// let (left, right) = v.split_at(2);
1142
- /// assert !(left == [1, 2]);
1143
- /// assert !(right == [3, 4, 5, 6]);
1146
+ /// assert_eq !(left, [1, 2]);
1147
+ /// assert_eq !(right, [3, 4, 5, 6]);
1144
1148
/// }
1145
1149
///
1146
1150
/// {
1147
1151
/// let (left, right) = v.split_at(6);
1148
- /// assert !(left == [1, 2, 3, 4, 5, 6]);
1149
- /// assert !(right == []);
1152
+ /// assert_eq !(left, [1, 2, 3, 4, 5, 6]);
1153
+ /// assert_eq !(right, []);
1150
1154
/// }
1151
1155
/// ```
1152
1156
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1175,12 +1179,12 @@ impl<T> [T] {
1175
1179
/// // scoped to restrict the lifetime of the borrows
1176
1180
/// {
1177
1181
/// let (left, right) = v.split_at_mut(2);
1178
- /// assert !(left == [1, 0]);
1179
- /// assert !(right == [3, 0, 5, 6]);
1182
+ /// assert_eq !(left, [1, 0]);
1183
+ /// assert_eq !(right, [3, 0, 5, 6]);
1180
1184
/// left[1] = 2;
1181
1185
/// right[1] = 4;
1182
1186
/// }
1183
- /// assert !(v == [1, 2, 3, 4, 5, 6]);
1187
+ /// assert_eq !(v, [1, 2, 3, 4, 5, 6]);
1184
1188
/// ```
1185
1189
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1186
1190
#[ inline]
@@ -1197,11 +1201,14 @@ impl<T> [T] {
1197
1201
/// the index `mid` itself) and the second will contain all
1198
1202
/// indices from `[mid, len)` (excluding the index `len` itself).
1199
1203
///
1200
- /// This is generally not recommended, use with caution!
1201
- /// Calling this method with an out-of-bounds index is *[undefined behavior]*
1202
- /// even if the resulting reference is not used.
1203
1204
/// For a safe alternative see [`split_at`].
1204
1205
///
1206
+ /// # Safety
1207
+ ///
1208
+ /// Calling this method with an out-of-bounds index is *[undefined behavior]*
1209
+ /// even if the resulting reference is not used. The caller has to ensure that
1210
+ /// `0 <= mid <= self.len()`.
1211
+ ///
1205
1212
/// [`split_at`]: #method.split_at
1206
1213
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1207
1214
///
@@ -1214,26 +1221,26 @@ impl<T> [T] {
1214
1221
///
1215
1222
/// unsafe {
1216
1223
/// let (left, right) = v.split_at_unchecked(0);
1217
- /// assert !(left == []);
1218
- /// assert !(right == [1, 2, 3, 4, 5, 6]);
1224
+ /// assert_eq !(left, []);
1225
+ /// assert_eq !(right, [1, 2, 3, 4, 5, 6]);
1219
1226
/// }
1220
1227
///
1221
1228
/// unsafe {
1222
1229
/// let (left, right) = v.split_at_unchecked(2);
1223
- /// assert !(left == [1, 2]);
1224
- /// assert !(right == [3, 4, 5, 6]);
1230
+ /// assert_eq !(left, [1, 2]);
1231
+ /// assert_eq !(right, [3, 4, 5, 6]);
1225
1232
/// }
1226
1233
///
1227
1234
/// unsafe {
1228
1235
/// let (left, right) = v.split_at_unchecked(6);
1229
- /// assert !(left == [1, 2, 3, 4, 5, 6]);
1230
- /// assert !(right == []);
1236
+ /// assert_eq !(left, [1, 2, 3, 4, 5, 6]);
1237
+ /// assert_eq !(right, []);
1231
1238
/// }
1232
1239
/// ```
1233
1240
#[ unstable( feature = "slice_split_at_unchecked" , reason = "new API" , issue = "76014" ) ]
1234
1241
#[ inline]
1235
1242
unsafe fn split_at_unchecked ( & self , mid : usize ) -> ( & [ T ] , & [ T ] ) {
1236
- // SAFETY: Caller has to check that 0 <= mid < self.len()
1243
+ // SAFETY: Caller has to check that ` 0 <= mid <= self.len()`
1237
1244
unsafe { ( self . get_unchecked ( ..mid) , self . get_unchecked ( mid..) ) }
1238
1245
}
1239
1246
@@ -1243,11 +1250,14 @@ impl<T> [T] {
1243
1250
/// the index `mid` itself) and the second will contain all
1244
1251
/// indices from `[mid, len)` (excluding the index `len` itself).
1245
1252
///
1246
- /// This is generally not recommended, use with caution!
1247
- /// Calling this method with an out-of-bounds index is *[undefined behavior]*
1248
- /// even if the resulting reference is not used.
1249
1253
/// For a safe alternative see [`split_at_mut`].
1250
1254
///
1255
+ /// # Safety
1256
+ ///
1257
+ /// Calling this method with an out-of-bounds index is *[undefined behavior]*
1258
+ /// even if the resulting reference is not used. The caller has to ensure that
1259
+ /// `0 <= mid <= self.len()`.
1260
+ ///
1251
1261
/// [`split_at_mut`]: #method.split_at_mut
1252
1262
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1253
1263
///
@@ -1260,22 +1270,22 @@ impl<T> [T] {
1260
1270
/// // scoped to restrict the lifetime of the borrows
1261
1271
/// unsafe {
1262
1272
/// let (left, right) = v.split_at_mut_unchecked(2);
1263
- /// assert !(left == [1, 0]);
1264
- /// assert !(right == [3, 0, 5, 6]);
1273
+ /// assert_eq !(left, [1, 0]);
1274
+ /// assert_eq !(right, [3, 0, 5, 6]);
1265
1275
/// left[1] = 2;
1266
1276
/// right[1] = 4;
1267
1277
/// }
1268
- /// assert !(v == [1, 2, 3, 4, 5, 6]);
1278
+ /// assert_eq !(v, [1, 2, 3, 4, 5, 6]);
1269
1279
/// ```
1270
1280
#[ unstable( feature = "slice_split_at_unchecked" , reason = "new API" , issue = "76014" ) ]
1271
1281
#[ inline]
1272
1282
unsafe fn split_at_mut_unchecked ( & mut self , mid : usize ) -> ( & mut [ T ] , & mut [ T ] ) {
1273
1283
let len = self . len ( ) ;
1274
1284
let ptr = self . as_mut_ptr ( ) ;
1275
1285
1276
- // SAFETY: Caller has to check that 0 <= mid < self.len().
1286
+ // SAFETY: Caller has to check that ` 0 <= mid <= self.len()` .
1277
1287
//
1278
- // [ptr; mid] and [mid; len] are not overlapping, so returning a mutable reference
1288
+ // ` [ptr; mid]` and ` [mid; len]` are not overlapping, so returning a mutable reference
1279
1289
// is fine.
1280
1290
unsafe { ( from_raw_parts_mut ( ptr, mid) , from_raw_parts_mut ( ptr. add ( mid) , len - mid) ) }
1281
1291
}
0 commit comments