@@ -986,11 +986,16 @@ impl<'a> cmp::Ord for Components<'a> {
986
986
// Basic types and traits
987
987
////////////////////////////////////////////////////////////////////////////////
988
988
989
- /// An owned, mutable path (akin to `String`).
989
+ /// An owned, mutable path (akin to [ `String`] ).
990
990
///
991
- /// This type provides methods like `push` and `set_extension` that mutate the
992
- /// path in place. It also implements `Deref` to `Path`, meaning that all
993
- /// methods on `Path` slices are available on `PathBuf` values as well.
991
+ /// This type provides methods like [`push`] and [`set_extension`] that mutate
992
+ /// the path in place. It also implements [`Deref`] to [`Path`], meaning that
993
+ /// all methods on [`Path`] slices are available on `PathBuf` values as well.
994
+ ///
995
+ /// [`String`]: ../string/struct.String.html
996
+ /// [`Path`]: struct.Path.html
997
+ /// [`push`]: struct.PathBuf.html#method.push
998
+ /// [`set_extension`]: struct.PathBuf.html#method.set_extension
994
999
///
995
1000
/// More details about the overall approach can be found in
996
1001
/// the module documentation.
@@ -1017,12 +1022,31 @@ impl PathBuf {
1017
1022
}
1018
1023
1019
1024
/// Allocates an empty `PathBuf`.
1025
+ ///
1026
+ /// # Examples
1027
+ ///
1028
+ /// ```
1029
+ /// use std::path::PathBuf;
1030
+ ///
1031
+ /// let path = PathBuf::new();
1032
+ /// ```
1020
1033
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1021
1034
pub fn new ( ) -> PathBuf {
1022
1035
PathBuf { inner : OsString :: new ( ) }
1023
1036
}
1024
1037
1025
- /// Coerces to a `Path` slice.
1038
+ /// Coerces to a [`Path`] slice.
1039
+ ///
1040
+ /// [`Path`]: struct.Path.html
1041
+ ///
1042
+ /// # Examples
1043
+ ///
1044
+ /// ```
1045
+ /// use std::path::{Path, PathBuf};
1046
+ ///
1047
+ /// let p = PathBuf::from("/test");
1048
+ /// assert_eq!(Path::new("/test"), p.as_path());
1049
+ /// ```
1026
1050
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1027
1051
pub fn as_path ( & self ) -> & Path {
1028
1052
self
@@ -1087,10 +1111,26 @@ impl PathBuf {
1087
1111
self . inner . push ( path) ;
1088
1112
}
1089
1113
1090
- /// Truncate `self` to `self.parent()`.
1114
+ /// Truncate `self` to [ `self.parent()`] .
1091
1115
///
1092
- /// Returns false and does nothing if `self.file_name()` is `None`.
1116
+ /// Returns false and does nothing if [ `self.file_name()`] is `None`.
1093
1117
/// Otherwise, returns `true`.
1118
+ ///
1119
+ /// [`self.parent()`]: struct.PathBuf.html#method.parent
1120
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1121
+ ///
1122
+ /// # Examples
1123
+ ///
1124
+ /// ```
1125
+ /// use std::path::PathBuf;
1126
+ ///
1127
+ /// let mut p = PathBuf::from("/test/test.rs");
1128
+ ///
1129
+ /// p.pop();
1130
+ /// assert_eq!(Path::new("/test"), p.as_path());
1131
+ /// p.pop();
1132
+ /// assert_eq!(Path::new("/"), p.as_path());
1133
+ /// ```
1094
1134
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1095
1135
pub fn pop ( & mut self ) -> bool {
1096
1136
match self . parent ( ) . map ( |p| p. as_u8_slice ( ) . len ( ) ) {
@@ -1102,11 +1142,13 @@ impl PathBuf {
1102
1142
}
1103
1143
}
1104
1144
1105
- /// Updates `self.file_name()` to `file_name`.
1145
+ /// Updates [ `self.file_name()`] to `file_name`.
1106
1146
///
1107
1147
/// If `self.file_name()` was `None`, this is equivalent to pushing
1108
1148
/// `file_name`.
1109
1149
///
1150
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1151
+ ///
1110
1152
/// # Examples
1111
1153
///
1112
1154
/// ```
@@ -1133,12 +1175,29 @@ impl PathBuf {
1133
1175
self . push ( file_name) ;
1134
1176
}
1135
1177
1136
- /// Updates `self.extension()` to `extension`.
1178
+ /// Updates [`self.extension()`] to `extension`.
1179
+ ///
1180
+ /// If [`self.file_name()`] is `None`, does nothing and returns `false`.
1181
+ ///
1182
+ /// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
1183
+ /// extension is added; otherwise it is replaced.
1184
+ ///
1185
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1186
+ /// [`self.extension()`]: struct.PathBuf.html#method.extension
1187
+ ///
1188
+ /// # Examples
1189
+ ///
1190
+ /// ```
1191
+ /// use std::path::PathBuf;
1137
1192
///
1138
- /// If `self.file_name()` is `None`, does nothing and returns `false`.
1193
+ /// let mut p = PathBuf::from("/feel/the");
1139
1194
///
1140
- /// Otherwise, returns `true`; if `self.extension()` is `None`, the extension
1141
- /// is added; otherwise it is replaced.
1195
+ /// p.set_extension("force");
1196
+ /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
1197
+ ///
1198
+ /// p.set_extension("dark_side");
1199
+ /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
1200
+ /// ```
1142
1201
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1143
1202
pub fn set_extension < S : AsRef < OsStr > > ( & mut self , extension : S ) -> bool {
1144
1203
self . _set_extension ( extension. as_ref ( ) )
@@ -1163,7 +1222,18 @@ impl PathBuf {
1163
1222
true
1164
1223
}
1165
1224
1166
- /// Consumes the `PathBuf`, yielding its internal `OsString` storage.
1225
+ /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
1226
+ ///
1227
+ /// [`OsString`]: ../ffi/struct.OsString.html
1228
+ ///
1229
+ /// # Examples
1230
+ ///
1231
+ /// ```
1232
+ /// use std::path::PathBuf;
1233
+ ///
1234
+ /// let p = PathBuf::from("/the/head");
1235
+ /// let os_str = p.into_os_string();
1236
+ /// ```
1167
1237
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1168
1238
pub fn into_os_string ( self ) -> OsString {
1169
1239
self . inner
@@ -1301,7 +1371,7 @@ impl Into<OsString> for PathBuf {
1301
1371
}
1302
1372
}
1303
1373
1304
- /// A slice of a path (akin to `str`).
1374
+ /// A slice of a path (akin to [ `str`] ).
1305
1375
///
1306
1376
/// This type supports a number of operations for inspecting a path, including
1307
1377
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1310,7 +1380,10 @@ impl Into<OsString> for PathBuf {
1310
1380
/// the module documentation.
1311
1381
///
1312
1382
/// This is an *unsized* type, meaning that it must always be used behind a
1313
- /// pointer like `&` or `Box`.
1383
+ /// pointer like `&` or [`Box`].
1384
+ ///
1385
+ /// [`str`]: ../primitive.str.html
1386
+ /// [`Box`]: ../boxed/struct.Box.html
1314
1387
///
1315
1388
/// # Examples
1316
1389
///
@@ -1372,7 +1445,9 @@ impl Path {
1372
1445
unsafe { mem:: transmute ( s. as_ref ( ) ) }
1373
1446
}
1374
1447
1375
- /// Yields the underlying `OsStr` slice.
1448
+ /// Yields the underlying [`OsStr`] slice.
1449
+ ///
1450
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
1376
1451
///
1377
1452
/// # Examples
1378
1453
///
@@ -1387,10 +1462,12 @@ impl Path {
1387
1462
& self . inner
1388
1463
}
1389
1464
1390
- /// Yields a `&str` slice if the `Path` is valid unicode.
1465
+ /// Yields a [ `&str`] slice if the `Path` is valid unicode.
1391
1466
///
1392
1467
/// This conversion may entail doing a check for UTF-8 validity.
1393
1468
///
1469
+ /// [`&str`]: ../primitive.str.html
1470
+ ///
1394
1471
/// # Examples
1395
1472
///
1396
1473
/// ```
@@ -1404,10 +1481,12 @@ impl Path {
1404
1481
self . inner . to_str ( )
1405
1482
}
1406
1483
1407
- /// Converts a `Path` to a `Cow<str>`.
1484
+ /// Converts a `Path` to a [ `Cow<str>`] .
1408
1485
///
1409
1486
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
1410
1487
///
1488
+ /// [`Cow<str>`]: ../borrow/enum.Cow.html
1489
+ ///
1411
1490
/// # Examples
1412
1491
///
1413
1492
/// ```
@@ -1421,7 +1500,9 @@ impl Path {
1421
1500
self . inner . to_string_lossy ( )
1422
1501
}
1423
1502
1424
- /// Converts a `Path` to an owned `PathBuf`.
1503
+ /// Converts a `Path` to an owned [`PathBuf`].
1504
+ ///
1505
+ /// [`PathBuf`]: struct.PathBuf.html
1425
1506
///
1426
1507
/// # Examples
1427
1508
///
@@ -1569,6 +1650,18 @@ impl Path {
1569
1650
///
1570
1651
/// If `base` is not a prefix of `self` (i.e. `starts_with`
1571
1652
/// returns `false`), returns `Err`.
1653
+ ///
1654
+ /// # Examples
1655
+ ///
1656
+ /// ```
1657
+ /// use std::path::Path;
1658
+ ///
1659
+ /// let path = Path::new("/test/haha/foo.txt");
1660
+ ///
1661
+ /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
1662
+ /// assert_eq!(path.strip_prefix("test").is_ok(), false);
1663
+ /// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
1664
+ /// ```
1572
1665
#[ stable( since = "1.7.0" , feature = "path_strip_prefix" ) ]
1573
1666
pub fn strip_prefix < ' a , P : ?Sized > ( & ' a self , base : & ' a P )
1574
1667
-> Result < & ' a Path , StripPrefixError >
@@ -1630,7 +1723,9 @@ impl Path {
1630
1723
iter_after ( self . components ( ) . rev ( ) , child. components ( ) . rev ( ) ) . is_some ( )
1631
1724
}
1632
1725
1633
- /// Extracts the stem (non-extension) portion of `self.file_name()`.
1726
+ /// Extracts the stem (non-extension) portion of [`self.file_name()`].
1727
+ ///
1728
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1634
1729
///
1635
1730
/// The stem is:
1636
1731
///
@@ -1653,7 +1748,9 @@ impl Path {
1653
1748
self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, after) | before. or ( after) )
1654
1749
}
1655
1750
1656
- /// Extracts the extension of `self.file_name()`, if possible.
1751
+ /// Extracts the extension of [`self.file_name()`], if possible.
1752
+ ///
1753
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1657
1754
///
1658
1755
/// The extension is:
1659
1756
///
@@ -1676,9 +1773,12 @@ impl Path {
1676
1773
self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, after) | before. and ( after) )
1677
1774
}
1678
1775
1679
- /// Creates an owned `PathBuf` with `path` adjoined to `self`.
1776
+ /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
1777
+ ///
1778
+ /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
1680
1779
///
1681
- /// See `PathBuf::push` for more details on what it means to adjoin a path.
1780
+ /// [`PathBuf`]: struct.PathBuf.html
1781
+ /// [`PathBuf::push`]: struct.PathBuf.html#method.push
1682
1782
///
1683
1783
/// # Examples
1684
1784
///
@@ -1698,9 +1798,12 @@ impl Path {
1698
1798
buf
1699
1799
}
1700
1800
1701
- /// Creates an owned `PathBuf` like `self` but with the given file name.
1801
+ /// Creates an owned [ `PathBuf`] like `self` but with the given file name.
1702
1802
///
1703
- /// See `PathBuf::set_file_name` for more details.
1803
+ /// See [`PathBuf::set_file_name`] for more details.
1804
+ ///
1805
+ /// [`PathBuf`]: struct.PathBuf.html
1806
+ /// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
1704
1807
///
1705
1808
/// # Examples
1706
1809
///
@@ -1721,9 +1824,12 @@ impl Path {
1721
1824
buf
1722
1825
}
1723
1826
1724
- /// Creates an owned `PathBuf` like `self` but with the given extension.
1827
+ /// Creates an owned [`PathBuf`] like `self` but with the given extension.
1828
+ ///
1829
+ /// See [`PathBuf::set_extension`] for more details.
1725
1830
///
1726
- /// See `PathBuf::set_extension` for more details.
1831
+ /// [`PathBuf`]: struct.PathBuf.html
1832
+ /// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
1727
1833
///
1728
1834
/// # Examples
1729
1835
///
@@ -1771,7 +1877,9 @@ impl Path {
1771
1877
}
1772
1878
}
1773
1879
1774
- /// Produce an iterator over the path's components viewed as `OsStr` slices.
1880
+ /// Produce an iterator over the path's components viewed as [`OsStr`] slices.
1881
+ ///
1882
+ /// [`OsStr`]: ffi/struct.OsStr.html
1775
1883
///
1776
1884
/// # Examples
1777
1885
///
@@ -1790,9 +1898,11 @@ impl Path {
1790
1898
Iter { inner : self . components ( ) }
1791
1899
}
1792
1900
1793
- /// Returns an object that implements `Display` for safely printing paths
1901
+ /// Returns an object that implements [ `Display`] for safely printing paths
1794
1902
/// that may contain non-Unicode data.
1795
1903
///
1904
+ /// [`Display`]: fmt/trait.Display.html
1905
+ ///
1796
1906
/// # Examples
1797
1907
///
1798
1908
/// ```
@@ -1854,11 +1964,13 @@ impl Path {
1854
1964
1855
1965
/// Returns an iterator over the entries within a directory.
1856
1966
///
1857
- /// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
1858
- /// be encountered after an iterator is initially constructed.
1967
+ /// The iterator will yield instances of [ `io::Result<`][` DirEntry`]` >`. New
1968
+ /// errors may be encountered after an iterator is initially constructed.
1859
1969
///
1860
1970
/// This is an alias to [`fs::read_dir`].
1861
1971
///
1972
+ /// [`io::Result<`]: ../io/type.Result.html
1973
+ /// [`DirEntry`]: ../fs/struct.DirEntry.html
1862
1974
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
1863
1975
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1864
1976
pub fn read_dir ( & self ) -> io:: Result < fs:: ReadDir > {
0 commit comments