@@ -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!
1452+ /// ```
1453+ ///
1454+ /// To work around this, we can use [`split_at_mut`] to create two distinct
1455+ /// sub-slices from a slice:
1456+ ///
14311457 /// ```
1432- /// let mut dst = [0, 0, 0];
1433- /// let src = [1, 2, 3];
1458+ /// let mut slice = [1, 2, 3, 4, 5];
14341459 ///
1435- /// dst.clone_from_slice(&src);
1436- /// assert!(dst == [1, 2, 3]);
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]);
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];
14601515 ///
1461- /// dst.copy_from_slice(&src);
1462- /// assert_eq!(src, dst);
1516+ /// {
1517+ /// let (left, right) = slice.split_at_mut(2);
1518+ /// left.copy_from_slice(&right[1..]);
1519+ /// }
1520+ ///
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+ ///
1543+ /// ```
1544+ /// #![feature(swap_with_slice)]
1545+ ///
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)]
1562+ ///
1563+ /// let mut slice = [1, 2, 3, 4, 5];
1564+ /// slice[..2].swap_with_slice(&mut slice[3..]); // compile fail!
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+ ///
14811570 /// ```
14821571 /// #![feature(swap_with_slice)]
14831572 ///
1484- /// let mut slice1 = [1, 2, 3];
1485- /// let mut slice2 = [7, 8, 9];
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+ /// }
14861579 ///
1487- /// slice1.swap_with_slice(&mut slice2);
1488- /// assert_eq!(slice1, [7, 8, 9]);
1489- /// assert_eq!(slice2, [1, 2, 3]);
1580+ /// assert_eq!(slice, [4, 5, 3, 1, 2]);
14901581 /// ```
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 , other : & mut [ T ] ) {
14931586 core_slice:: SliceExt :: swap_with_slice ( self , other)
@@ -1626,120 +1719,6 @@ impl [u8] {
16261719 byte. make_ascii_lowercase ( ) ;
16271720 }
16281721 }
1629-
1630- /// Checks if all bytes of this slice are ASCII alphabetic characters:
1631- ///
1632- /// - U+0041 'A' ... U+005A 'Z', or
1633- /// - U+0061 'a' ... U+007A 'z'.
1634- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1635- #[ inline]
1636- pub fn is_ascii_alphabetic ( & self ) -> bool {
1637- self . iter ( ) . all ( |b| b. is_ascii_alphabetic ( ) )
1638- }
1639-
1640- /// Checks if all bytes of this slice are ASCII uppercase characters:
1641- /// U+0041 'A' ... U+005A 'Z'.
1642- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1643- #[ inline]
1644- pub fn is_ascii_uppercase ( & self ) -> bool {
1645- self . iter ( ) . all ( |b| b. is_ascii_uppercase ( ) )
1646- }
1647-
1648- /// Checks if all bytes of this slice are ASCII lowercase characters:
1649- /// U+0061 'a' ... U+007A 'z'.
1650- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1651- #[ inline]
1652- pub fn is_ascii_lowercase ( & self ) -> bool {
1653- self . iter ( ) . all ( |b| b. is_ascii_lowercase ( ) )
1654- }
1655-
1656- /// Checks if all bytes of this slice are ASCII alphanumeric characters:
1657- ///
1658- /// - U+0041 'A' ... U+005A 'Z', or
1659- /// - U+0061 'a' ... U+007A 'z', or
1660- /// - U+0030 '0' ... U+0039 '9'.
1661- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1662- #[ inline]
1663- pub fn is_ascii_alphanumeric ( & self ) -> bool {
1664- self . iter ( ) . all ( |b| b. is_ascii_alphanumeric ( ) )
1665- }
1666-
1667- /// Checks if all bytes of this slice are ASCII decimal digit:
1668- /// U+0030 '0' ... U+0039 '9'.
1669- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1670- #[ inline]
1671- pub fn is_ascii_digit ( & self ) -> bool {
1672- self . iter ( ) . all ( |b| b. is_ascii_digit ( ) )
1673- }
1674-
1675- /// Checks if all bytes of this slice are ASCII hexadecimal digits:
1676- ///
1677- /// - U+0030 '0' ... U+0039 '9', or
1678- /// - U+0041 'A' ... U+0046 'F', or
1679- /// - U+0061 'a' ... U+0066 'f'.
1680- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1681- #[ inline]
1682- pub fn is_ascii_hexdigit ( & self ) -> bool {
1683- self . iter ( ) . all ( |b| b. is_ascii_hexdigit ( ) )
1684- }
1685-
1686- /// Checks if all bytes of this slice are ASCII punctuation characters:
1687- ///
1688- /// - U+0021 ... U+002F `! " # $ % & ' ( ) * + , - . /`, or
1689- /// - U+003A ... U+0040 `: ; < = > ? @`, or
1690- /// - U+005B ... U+0060 `[ \\ ] ^ _ \``, or
1691- /// - U+007B ... U+007E `{ | } ~`
1692- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1693- #[ inline]
1694- pub fn is_ascii_punctuation ( & self ) -> bool {
1695- self . iter ( ) . all ( |b| b. is_ascii_punctuation ( ) )
1696- }
1697-
1698- /// Checks if all bytes of this slice are ASCII graphic characters:
1699- /// U+0021 '@' ... U+007E '~'.
1700- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1701- #[ inline]
1702- pub fn is_ascii_graphic ( & self ) -> bool {
1703- self . iter ( ) . all ( |b| b. is_ascii_graphic ( ) )
1704- }
1705-
1706- /// Checks if all bytes of this slice are ASCII whitespace characters:
1707- /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
1708- /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
1709- ///
1710- /// Rust uses the WhatWG Infra Standard's [definition of ASCII
1711- /// whitespace][infra-aw]. There are several other definitions in
1712- /// wide use. For instance, [the POSIX locale][pct] includes
1713- /// U+000B VERTICAL TAB as well as all the above characters,
1714- /// but—from the very same specification—[the default rule for
1715- /// "field splitting" in the Bourne shell][bfs] considers *only*
1716- /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
1717- ///
1718- /// If you are writing a program that will process an existing
1719- /// file format, check what that format's definition of whitespace is
1720- /// before using this function.
1721- ///
1722- /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
1723- /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
1724- /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
1725- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1726- #[ inline]
1727- pub fn is_ascii_whitespace ( & self ) -> bool {
1728- self . iter ( ) . all ( |b| b. is_ascii_whitespace ( ) )
1729- }
1730-
1731- /// Checks if all bytes of this slice are ASCII control characters:
1732- ///
1733- /// - U+0000 NUL ... U+001F UNIT SEPARATOR, or
1734- /// - U+007F DELETE.
1735- ///
1736- /// Note that most ASCII whitespace characters are control
1737- /// characters, but SPACE is not.
1738- #[ unstable( feature = "ascii_ctype" , issue = "39658" ) ]
1739- #[ inline]
1740- pub fn is_ascii_control ( & self ) -> bool {
1741- self . iter ( ) . all ( |b| b. is_ascii_control ( ) )
1742- }
17431722}
17441723
17451724////////////////////////////////////////////////////////////////////////////////
0 commit comments