@@ -1428,15 +1428,45 @@ impl<T> [T] {
1428
1428
///
1429
1429
/// # Examples
1430
1430
///
1431
+ /// Cloning two elements from a slice into another:
1432
+ ///
1433
+ /// ```
1434
+ /// let src = [1, 2, 3, 4];
1435
+ /// let mut dst = [0, 0];
1436
+ ///
1437
+ /// dst.clone_from_slice(&src[2..]);
1438
+ ///
1439
+ /// assert_eq!(src, [1, 2, 3, 4]);
1440
+ /// assert_eq!(dst, [3, 4]);
1441
+ /// ```
1442
+ ///
1443
+ /// Rust enforces that there can only be one mutable reference with no
1444
+ /// immutable references to a particular piece of data in a particular
1445
+ /// scope. Because of this, attempting to use `clone_from_slice` on a
1446
+ /// single slice will result in a compile failure:
1447
+ ///
1448
+ /// ```compile_fail
1449
+ /// let mut slice = [1, 2, 3, 4, 5];
1450
+ ///
1451
+ /// slice[..2].clone_from_slice(&slice[3..]); // compile fail!
1431
1452
/// ```
1432
- /// let mut dst = [0, 0, 0];
1433
- /// let src = [1, 2, 3];
1434
1453
///
1435
- /// dst.clone_from_slice(&src);
1436
- /// assert!(dst == [1, 2, 3]);
1454
+ /// To work around this, we can use [`split_at_mut`] to create two distinct
1455
+ /// sub-slices from a slice:
1456
+ ///
1457
+ /// ```
1458
+ /// let mut slice = [1, 2, 3, 4, 5];
1459
+ ///
1460
+ /// {
1461
+ /// let (left, right) = slice.split_at_mut(2);
1462
+ /// left.clone_from_slice(&right[1..]);
1463
+ /// }
1464
+ ///
1465
+ /// assert_eq!(slice, [4, 5, 3, 4, 5]);
1437
1466
/// ```
1438
1467
///
1439
1468
/// [`copy_from_slice`]: #method.copy_from_slice
1469
+ /// [`split_at_mut`]: #method.split_at_mut
1440
1470
#[ stable( feature = "clone_from_slice" , since = "1.7.0" ) ]
1441
1471
pub fn clone_from_slice ( & mut self , src : & [ T ] ) where T : Clone {
1442
1472
core_slice:: SliceExt :: clone_from_slice ( self , src)
@@ -1454,15 +1484,45 @@ impl<T> [T] {
1454
1484
///
1455
1485
/// # Examples
1456
1486
///
1487
+ /// Copying two elements from a slice into another:
1488
+ ///
1489
+ /// ```
1490
+ /// let src = [1, 2, 3, 4];
1491
+ /// let mut dst = [0, 0];
1492
+ ///
1493
+ /// dst.copy_from_slice(&src[2..]);
1494
+ ///
1495
+ /// assert_eq!(src, [1, 2, 3, 4]);
1496
+ /// assert_eq!(dst, [3, 4]);
1497
+ /// ```
1498
+ ///
1499
+ /// Rust enforces that there can only be one mutable reference with no
1500
+ /// immutable references to a particular piece of data in a particular
1501
+ /// scope. Because of this, attempting to use `copy_from_slice` on a
1502
+ /// single slice will result in a compile failure:
1503
+ ///
1504
+ /// ```compile_fail
1505
+ /// let mut slice = [1, 2, 3, 4, 5];
1506
+ ///
1507
+ /// slice[..2].copy_from_slice(&slice[3..]); // compile fail!
1508
+ /// ```
1509
+ ///
1510
+ /// To work around this, we can use [`split_at_mut`] to create two distinct
1511
+ /// sub-slices from a slice:
1512
+ ///
1457
1513
/// ```
1458
- /// let mut dst = [0, 0, 0];
1459
- /// let src = [1, 2, 3];
1514
+ /// let mut slice = [1, 2, 3, 4, 5];
1515
+ ///
1516
+ /// {
1517
+ /// let (left, right) = slice.split_at_mut(2);
1518
+ /// left.copy_from_slice(&right[1..]);
1519
+ /// }
1460
1520
///
1461
- /// dst.copy_from_slice(&src);
1462
- /// assert_eq!(src, dst);
1521
+ /// assert_eq!(slice, [4, 5, 3, 4, 5]);
1463
1522
/// ```
1464
1523
///
1465
1524
/// [`clone_from_slice`]: #method.clone_from_slice
1525
+ /// [`split_at_mut`]: #method.split_at_mut
1466
1526
#[ stable( feature = "copy_from_slice" , since = "1.9.0" ) ]
1467
1527
pub fn copy_from_slice ( & mut self , src : & [ T ] ) where T : Copy {
1468
1528
core_slice:: SliceExt :: copy_from_slice ( self , src)
@@ -1478,16 +1538,49 @@ impl<T> [T] {
1478
1538
///
1479
1539
/// # Example
1480
1540
///
1541
+ /// Swapping two elements across slices:
1542
+ ///
1481
1543
/// ```
1482
1544
/// #![feature(swap_with_slice)]
1483
1545
///
1484
- /// let mut slice1 = [1, 2, 3];
1485
- /// let mut slice2 = [7, 8, 9];
1546
+ /// let mut slice1 = [0, 0];
1547
+ /// let mut slice2 = [1, 2, 3, 4];
1548
+ ///
1549
+ /// slice1.swap_with_slice(&mut slice2[2..]);
1550
+ ///
1551
+ /// assert_eq!(slice1, [3, 4]);
1552
+ /// assert_eq!(slice2, [1, 2, 0, 0]);
1553
+ /// ```
1554
+ ///
1555
+ /// Rust enforces that there can only be one mutable reference to a
1556
+ /// particular piece of data in a particular scope. Because of this,
1557
+ /// attempting to use `swap_with_slice` on a single slice will result in
1558
+ /// a compile failure:
1559
+ ///
1560
+ /// ```compile_fail
1561
+ /// #![feature(swap_with_slice)]
1486
1562
///
1487
- /// slice1.swap_with_slice(&mut slice2);
1488
- /// assert_eq!(slice1, [7, 8, 9]);
1489
- /// assert_eq!(slice2, [1, 2, 3]);
1563
+ /// let mut slice = [1, 2, 3, 4, 5];
1564
+ /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
1490
1565
/// ```
1566
+ ///
1567
+ /// To work around this, we can use [`split_at_mut`] to create two distinct
1568
+ /// mutable sub-slices from a slice:
1569
+ ///
1570
+ /// ```
1571
+ /// #![feature(swap_with_slice)]
1572
+ ///
1573
+ /// let mut slice = [1, 2, 3, 4, 5];
1574
+ ///
1575
+ /// {
1576
+ /// let (left, right) = slice.split_at_mut(2);
1577
+ /// left.swap_with_slice(&mut right[1..]);
1578
+ /// }
1579
+ ///
1580
+ /// assert_eq!(slice, [4, 5, 3, 1, 2]);
1581
+ /// ```
1582
+ ///
1583
+ /// [`split_at_mut`]: #method.split_at_mut
1491
1584
#[ unstable( feature = "swap_with_slice" , issue = "44030" ) ]
1492
1585
pub fn swap_with_slice ( & mut self , other : & mut [ T ] ) {
1493
1586
core_slice:: SliceExt :: swap_with_slice ( self , other)
0 commit comments