@@ -1360,24 +1360,61 @@ impl<T> [T] {
1360
1360
core_slice:: SliceExt :: sort_unstable_by_key ( self , f) ;
1361
1361
}
1362
1362
1363
- /// Permutes the slice in-place such that `self[ mid..]` moves to the
1364
- /// beginning of the slice while `self[.. mid]` moves to the end of the
1365
- /// slice. Equivalently, rotates the slice `mid` places to the left
1366
- /// or `k = self.len() - mid` places to the right .
1363
+ /// Rotates the slice in-place such that the first ` mid` elements of the
1364
+ /// slice move to the end while the last `self.len() - mid` elements move to
1365
+ /// the front. After calling `rotate_left`, the element previously at index
1366
+ /// `mid` will become the first element in the slice .
1367
1367
///
1368
- /// This is a "k-rotation", a permutation in which item `i` moves to
1369
- /// position `i + k`, modulo the length of the slice. See _Elements
1370
- /// of Programming_ [§10.4][eop].
1368
+ /// # Panics
1369
+ ///
1370
+ /// This function will panic if `mid` is greater than the length of the
1371
+ /// slice. Note that `mid == self.len()` does _not_ panic and is a no-op
1372
+ /// rotation.
1373
+ ///
1374
+ /// # Complexity
1375
+ ///
1376
+ /// Takes linear (in `self.len()`) time.
1377
+ ///
1378
+ /// # Examples
1371
1379
///
1372
- /// Rotation by `mid` and rotation by `k` are inverse operations.
1380
+ /// ```
1381
+ /// #![feature(slice_rotate)]
1373
1382
///
1374
- /// [eop]: https://books.google.com/books?id=CO9ULZGINlsC&pg=PA178&q=k-rotation
1383
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1384
+ /// a.rotate_left(2);
1385
+ /// assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
1386
+ /// ```
1387
+ ///
1388
+ /// Rotating a subslice:
1389
+ ///
1390
+ /// ```
1391
+ /// #![feature(slice_rotate)]
1392
+ ///
1393
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1394
+ /// a[1..5].rotate_left(1);
1395
+ /// assert_eq!(a, ['a', 'c', 'd', 'e', 'b', 'f']);
1396
+ /// ```
1397
+ #[ unstable( feature = "slice_rotate" , issue = "41891" ) ]
1398
+ pub fn rotate_left ( & mut self , mid : usize ) {
1399
+ core_slice:: SliceExt :: rotate_left ( self , mid) ;
1400
+ }
1401
+
1402
+ #[ unstable( feature = "slice_rotate" , issue = "41891" ) ]
1403
+ #[ rustc_deprecated( since = "" , reason = "renamed to `rotate_left`" ) ]
1404
+ pub fn rotate ( & mut self , mid : usize ) {
1405
+ core_slice:: SliceExt :: rotate_left ( self , mid) ;
1406
+ }
1407
+
1408
+ /// Rotates the slice in-place such that the first `self.len() - k`
1409
+ /// elements of the slice move to the end while the last `k` elements move
1410
+ /// to the front. After calling `rotate_right`, the element previously at
1411
+ /// index `self.len() - k` will become the first element in the slice.
1375
1412
///
1376
1413
/// # Panics
1377
1414
///
1378
- /// This function will panic if `mid ` is greater than the length of the
1379
- /// slice. ( Note that `mid == self.len()` does _not_ panic; it's a nop
1380
- /// rotation with `k == 0`, the inverse of a rotation with `mid == 0`.)
1415
+ /// This function will panic if `k ` is greater than the length of the
1416
+ /// slice. Note that `k == self.len()` does _not_ panic and is a no-op
1417
+ /// rotation.
1381
1418
///
1382
1419
/// # Complexity
1383
1420
///
@@ -1388,31 +1425,23 @@ impl<T> [T] {
1388
1425
/// ```
1389
1426
/// #![feature(slice_rotate)]
1390
1427
///
1391
- /// let mut a = [1, 2, 3, 4, 5, 6, 7];
1392
- /// let mid = 2;
1393
- /// a.rotate(mid);
1394
- /// assert_eq!(&a, &[3, 4, 5, 6, 7, 1, 2]);
1395
- /// let k = a.len() - mid;
1396
- /// a.rotate(k);
1397
- /// assert_eq!(&a, &[1, 2, 3, 4, 5, 6, 7]);
1398
- ///
1399
- /// use std::ops::Range;
1400
- /// fn slide<T>(slice: &mut [T], range: Range<usize>, to: usize) {
1401
- /// if to < range.start {
1402
- /// slice[to..range.end].rotate(range.start-to);
1403
- /// } else if to > range.end {
1404
- /// slice[range.start..to].rotate(range.end-range.start);
1405
- /// }
1406
- /// }
1407
- /// let mut v: Vec<_> = (0..10).collect();
1408
- /// slide(&mut v, 1..4, 7);
1409
- /// assert_eq!(&v, &[0, 4, 5, 6, 1, 2, 3, 7, 8, 9]);
1410
- /// slide(&mut v, 6..8, 1);
1411
- /// assert_eq!(&v, &[0, 3, 7, 4, 5, 6, 1, 2, 8, 9]);
1428
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1429
+ /// a.rotate_right(2);
1430
+ /// assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
1431
+ /// ```
1432
+ ///
1433
+ /// Rotate a subslice:
1434
+ ///
1435
+ /// ```
1436
+ /// #![feature(slice_rotate)]
1437
+ ///
1438
+ /// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1439
+ /// a[1..5].rotate_right(1);
1440
+ /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
1412
1441
/// ```
1413
1442
#[ unstable( feature = "slice_rotate" , issue = "41891" ) ]
1414
- pub fn rotate ( & mut self , mid : usize ) {
1415
- core_slice:: SliceExt :: rotate ( self , mid ) ;
1443
+ pub fn rotate_right ( & mut self , k : usize ) {
1444
+ core_slice:: SliceExt :: rotate_right ( self , k ) ;
1416
1445
}
1417
1446
1418
1447
/// Copies the elements from `src` into `self`.
0 commit comments