@@ -1428,15 +1428,45 @@ impl<T> [T] {
14281428 ///
14291429 /// # Examples
14301430 ///
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!
14311452 /// ```
1432- /// let mut dst = [0, 0, 0];
1433- /// let src = [1, 2, 3];
14341453 ///
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[..2].clone_from_slice(&right[1..]);
1463+ /// }
1464+ ///
1465+ /// assert_eq!(slice, [4, 5, 3, 4, 5]);
14371466 /// ```
14381467 ///
14391468 /// [`copy_from_slice`]: #method.copy_from_slice
1469+ /// [`split_at_mut`]: #method.split_at_mut
14401470 #[ stable( feature = "clone_from_slice" , since = "1.7.0" ) ]
14411471 pub fn clone_from_slice ( & mut self , src : & [ T ] ) where T : Clone {
14421472 core_slice:: SliceExt :: clone_from_slice ( self , src)
@@ -1454,15 +1484,45 @@ impl<T> [T] {
14541484 ///
14551485 /// # Examples
14561486 ///
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+ ///
14571513 /// ```
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[..2].copy_from_slice(&right[1..]);
1519+ /// }
14601520 ///
1461- /// dst.copy_from_slice(&src);
1462- /// assert_eq!(src, dst);
1521+ /// assert_eq!(slice, [4, 5, 3, 4, 5]);
14631522 /// ```
14641523 ///
14651524 /// [`clone_from_slice`]: #method.clone_from_slice
1525+ /// [`split_at_mut`]: #method.split_at_mut
14661526 #[ stable( feature = "copy_from_slice" , since = "1.9.0" ) ]
14671527 pub fn copy_from_slice ( & mut self , src : & [ T ] ) where T : Copy {
14681528 core_slice:: SliceExt :: copy_from_slice ( self , src)
@@ -1478,16 +1538,49 @@ impl<T> [T] {
14781538 ///
14791539 /// # Example
14801540 ///
1541+ /// Swapping two elements across slices:
1542+ ///
14811543 /// ```
14821544 /// #![feature(swap_with_slice)]
14831545 ///
1484- /// let mut src = [1, 2, 3];
1485- /// let mut dst = [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)]
14861562 ///
1487- /// src.swap_with_slice(&mut dst);
1488- /// assert_eq!(src, [7, 8, 9]);
1489- /// assert_eq!(dst, [1, 2, 3]);
1563+ /// let mut slice = [1, 2, 3, 4, 5];
1564+ /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
14901565 /// ```
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[..2].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
14911584 #[ unstable( feature = "swap_with_slice" , issue = "44030" ) ]
14921585 pub fn swap_with_slice ( & mut self , src : & mut [ T ] ) {
14931586 core_slice:: SliceExt :: swap_with_slice ( self , src)
0 commit comments