@@ -1028,11 +1028,16 @@ impl<'a> cmp::Ord for Components<'a> {
1028
1028
// Basic types and traits
1029
1029
////////////////////////////////////////////////////////////////////////////////
1030
1030
1031
- /// An owned, mutable path (akin to `String`).
1031
+ /// An owned, mutable path (akin to [ `String`] ).
1032
1032
///
1033
- /// This type provides methods like `push` and `set_extension` that mutate the
1034
- /// path in place. It also implements `Deref` to `Path`, meaning that all
1035
- /// methods on `Path` slices are available on `PathBuf` values as well.
1033
+ /// This type provides methods like [`push`] and [`set_extension`] that mutate
1034
+ /// the path in place. It also implements [`Deref`] to [`Path`], meaning that
1035
+ /// all methods on [`Path`] slices are available on `PathBuf` values as well.
1036
+ ///
1037
+ /// [`String`]: ../string/struct.String.html
1038
+ /// [`Path`]: struct.Path.html
1039
+ /// [`push`]: struct.PathBuf.html#method.push
1040
+ /// [`set_extension`]: struct.PathBuf.html#method.set_extension
1036
1041
///
1037
1042
/// More details about the overall approach can be found in
1038
1043
/// the module documentation.
@@ -1059,12 +1064,31 @@ impl PathBuf {
1059
1064
}
1060
1065
1061
1066
/// Allocates an empty `PathBuf`.
1067
+ ///
1068
+ /// # Examples
1069
+ ///
1070
+ /// ```
1071
+ /// use std::path::PathBuf;
1072
+ ///
1073
+ /// let path = PathBuf::new();
1074
+ /// ```
1062
1075
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1063
1076
pub fn new ( ) -> PathBuf {
1064
1077
PathBuf { inner : OsString :: new ( ) }
1065
1078
}
1066
1079
1067
- /// Coerces to a `Path` slice.
1080
+ /// Coerces to a [`Path`] slice.
1081
+ ///
1082
+ /// [`Path`]: struct.Path.html
1083
+ ///
1084
+ /// # Examples
1085
+ ///
1086
+ /// ```
1087
+ /// use std::path::{Path, PathBuf};
1088
+ ///
1089
+ /// let p = PathBuf::from("/test");
1090
+ /// assert_eq!(Path::new("/test"), p.as_path());
1091
+ /// ```
1068
1092
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1069
1093
pub fn as_path ( & self ) -> & Path {
1070
1094
self
@@ -1129,10 +1153,26 @@ impl PathBuf {
1129
1153
self . inner . push ( path) ;
1130
1154
}
1131
1155
1132
- /// Truncate `self` to `self.parent()`.
1156
+ /// Truncate `self` to [ `self.parent()`] .
1133
1157
///
1134
- /// Returns false and does nothing if `self.file_name()` is `None`.
1158
+ /// Returns false and does nothing if [ `self.file_name()`] is `None`.
1135
1159
/// Otherwise, returns `true`.
1160
+ ///
1161
+ /// [`self.parent()`]: struct.PathBuf.html#method.parent
1162
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1163
+ ///
1164
+ /// # Examples
1165
+ ///
1166
+ /// ```
1167
+ /// use std::path::{Path, PathBuf};
1168
+ ///
1169
+ /// let mut p = PathBuf::from("/test/test.rs");
1170
+ ///
1171
+ /// p.pop();
1172
+ /// assert_eq!(Path::new("/test"), p.as_path());
1173
+ /// p.pop();
1174
+ /// assert_eq!(Path::new("/"), p.as_path());
1175
+ /// ```
1136
1176
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1137
1177
pub fn pop ( & mut self ) -> bool {
1138
1178
match self . parent ( ) . map ( |p| p. as_u8_slice ( ) . len ( ) ) {
@@ -1144,11 +1184,13 @@ impl PathBuf {
1144
1184
}
1145
1185
}
1146
1186
1147
- /// Updates `self.file_name()` to `file_name`.
1187
+ /// Updates [ `self.file_name()`] to `file_name`.
1148
1188
///
1149
- /// If `self.file_name()` was `None`, this is equivalent to pushing
1189
+ /// If [ `self.file_name()`] was `None`, this is equivalent to pushing
1150
1190
/// `file_name`.
1151
1191
///
1192
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1193
+ ///
1152
1194
/// # Examples
1153
1195
///
1154
1196
/// ```
@@ -1175,12 +1217,29 @@ impl PathBuf {
1175
1217
self . push ( file_name) ;
1176
1218
}
1177
1219
1178
- /// Updates `self.extension()` to `extension`.
1220
+ /// Updates [`self.extension()`] to `extension`.
1221
+ ///
1222
+ /// If [`self.file_name()`] is `None`, does nothing and returns `false`.
1223
+ ///
1224
+ /// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
1225
+ /// extension is added; otherwise it is replaced.
1226
+ ///
1227
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1228
+ /// [`self.extension()`]: struct.PathBuf.html#method.extension
1229
+ ///
1230
+ /// # Examples
1231
+ ///
1232
+ /// ```
1233
+ /// use std::path::{Path, PathBuf};
1179
1234
///
1180
- /// If `self.file_name()` is `None`, does nothing and returns `false`.
1235
+ /// let mut p = PathBuf::from("/feel/the");
1181
1236
///
1182
- /// Otherwise, returns `true`; if `self.extension()` is `None`, the extension
1183
- /// is added; otherwise it is replaced.
1237
+ /// p.set_extension("force");
1238
+ /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
1239
+ ///
1240
+ /// p.set_extension("dark_side");
1241
+ /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
1242
+ /// ```
1184
1243
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1185
1244
pub fn set_extension < S : AsRef < OsStr > > ( & mut self , extension : S ) -> bool {
1186
1245
self . _set_extension ( extension. as_ref ( ) )
@@ -1205,7 +1264,18 @@ impl PathBuf {
1205
1264
true
1206
1265
}
1207
1266
1208
- /// Consumes the `PathBuf`, yielding its internal `OsString` storage.
1267
+ /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
1268
+ ///
1269
+ /// [`OsString`]: ../ffi/struct.OsString.html
1270
+ ///
1271
+ /// # Examples
1272
+ ///
1273
+ /// ```
1274
+ /// use std::path::PathBuf;
1275
+ ///
1276
+ /// let p = PathBuf::from("/the/head");
1277
+ /// let os_str = p.into_os_string();
1278
+ /// ```
1209
1279
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1210
1280
pub fn into_os_string ( self ) -> OsString {
1211
1281
self . inner
@@ -1343,7 +1413,7 @@ impl Into<OsString> for PathBuf {
1343
1413
}
1344
1414
}
1345
1415
1346
- /// A slice of a path (akin to `str`).
1416
+ /// A slice of a path (akin to [ `str`] ).
1347
1417
///
1348
1418
/// This type supports a number of operations for inspecting a path, including
1349
1419
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1352,7 +1422,10 @@ impl Into<OsString> for PathBuf {
1352
1422
/// the module documentation.
1353
1423
///
1354
1424
/// This is an *unsized* type, meaning that it must always be used behind a
1355
- /// pointer like `&` or `Box`.
1425
+ /// pointer like `&` or [`Box`].
1426
+ ///
1427
+ /// [`str`]: ../primitive.str.html
1428
+ /// [`Box`]: ../boxed/struct.Box.html
1356
1429
///
1357
1430
/// # Examples
1358
1431
///
@@ -1414,7 +1487,9 @@ impl Path {
1414
1487
unsafe { mem:: transmute ( s. as_ref ( ) ) }
1415
1488
}
1416
1489
1417
- /// Yields the underlying `OsStr` slice.
1490
+ /// Yields the underlying [`OsStr`] slice.
1491
+ ///
1492
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
1418
1493
///
1419
1494
/// # Examples
1420
1495
///
@@ -1429,10 +1504,12 @@ impl Path {
1429
1504
& self . inner
1430
1505
}
1431
1506
1432
- /// Yields a `&str` slice if the `Path` is valid unicode.
1507
+ /// Yields a [ `&str`] slice if the `Path` is valid unicode.
1433
1508
///
1434
1509
/// This conversion may entail doing a check for UTF-8 validity.
1435
1510
///
1511
+ /// [`&str`]: ../primitive.str.html
1512
+ ///
1436
1513
/// # Examples
1437
1514
///
1438
1515
/// ```
@@ -1446,10 +1523,12 @@ impl Path {
1446
1523
self . inner . to_str ( )
1447
1524
}
1448
1525
1449
- /// Converts a `Path` to a `Cow<str>`.
1526
+ /// Converts a `Path` to a [ `Cow<str>`] .
1450
1527
///
1451
1528
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
1452
1529
///
1530
+ /// [`Cow<str>`]: ../borrow/enum.Cow.html
1531
+ ///
1453
1532
/// # Examples
1454
1533
///
1455
1534
/// ```
@@ -1463,7 +1542,9 @@ impl Path {
1463
1542
self . inner . to_string_lossy ( )
1464
1543
}
1465
1544
1466
- /// Converts a `Path` to an owned `PathBuf`.
1545
+ /// Converts a `Path` to an owned [`PathBuf`].
1546
+ ///
1547
+ /// [`PathBuf`]: struct.PathBuf.html
1467
1548
///
1468
1549
/// # Examples
1469
1550
///
@@ -1611,6 +1692,18 @@ impl Path {
1611
1692
///
1612
1693
/// If `base` is not a prefix of `self` (i.e. `starts_with`
1613
1694
/// returns `false`), returns `Err`.
1695
+ ///
1696
+ /// # Examples
1697
+ ///
1698
+ /// ```
1699
+ /// use std::path::Path;
1700
+ ///
1701
+ /// let path = Path::new("/test/haha/foo.txt");
1702
+ ///
1703
+ /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
1704
+ /// assert_eq!(path.strip_prefix("test").is_ok(), false);
1705
+ /// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
1706
+ /// ```
1614
1707
#[ stable( since = "1.7.0" , feature = "path_strip_prefix" ) ]
1615
1708
pub fn strip_prefix < ' a , P : ?Sized > ( & ' a self , base : & ' a P )
1616
1709
-> Result < & ' a Path , StripPrefixError >
@@ -1672,7 +1765,9 @@ impl Path {
1672
1765
iter_after ( self . components ( ) . rev ( ) , child. components ( ) . rev ( ) ) . is_some ( )
1673
1766
}
1674
1767
1675
- /// Extracts the stem (non-extension) portion of `self.file_name()`.
1768
+ /// Extracts the stem (non-extension) portion of [`self.file_name()`].
1769
+ ///
1770
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1676
1771
///
1677
1772
/// The stem is:
1678
1773
///
@@ -1695,7 +1790,9 @@ impl Path {
1695
1790
self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, after) | before. or ( after) )
1696
1791
}
1697
1792
1698
- /// Extracts the extension of `self.file_name()`, if possible.
1793
+ /// Extracts the extension of [`self.file_name()`], if possible.
1794
+ ///
1795
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1699
1796
///
1700
1797
/// The extension is:
1701
1798
///
@@ -1718,9 +1815,12 @@ impl Path {
1718
1815
self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, after) | before. and ( after) )
1719
1816
}
1720
1817
1721
- /// Creates an owned `PathBuf` with `path` adjoined to `self`.
1818
+ /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
1819
+ ///
1820
+ /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
1722
1821
///
1723
- /// See `PathBuf::push` for more details on what it means to adjoin a path.
1822
+ /// [`PathBuf`]: struct.PathBuf.html
1823
+ /// [`PathBuf::push`]: struct.PathBuf.html#method.push
1724
1824
///
1725
1825
/// # Examples
1726
1826
///
@@ -1740,9 +1840,12 @@ impl Path {
1740
1840
buf
1741
1841
}
1742
1842
1743
- /// Creates an owned `PathBuf` like `self` but with the given file name.
1843
+ /// Creates an owned [ `PathBuf`] like `self` but with the given file name.
1744
1844
///
1745
- /// See `PathBuf::set_file_name` for more details.
1845
+ /// See [`PathBuf::set_file_name`] for more details.
1846
+ ///
1847
+ /// [`PathBuf`]: struct.PathBuf.html
1848
+ /// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
1746
1849
///
1747
1850
/// # Examples
1748
1851
///
@@ -1763,9 +1866,12 @@ impl Path {
1763
1866
buf
1764
1867
}
1765
1868
1766
- /// Creates an owned `PathBuf` like `self` but with the given extension.
1869
+ /// Creates an owned [`PathBuf`] like `self` but with the given extension.
1870
+ ///
1871
+ /// See [`PathBuf::set_extension`] for more details.
1767
1872
///
1768
- /// See `PathBuf::set_extension` for more details.
1873
+ /// [`PathBuf`]: struct.PathBuf.html
1874
+ /// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
1769
1875
///
1770
1876
/// # Examples
1771
1877
///
@@ -1813,7 +1919,9 @@ impl Path {
1813
1919
}
1814
1920
}
1815
1921
1816
- /// Produce an iterator over the path's components viewed as `OsStr` slices.
1922
+ /// Produce an iterator over the path's components viewed as [`OsStr`] slices.
1923
+ ///
1924
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
1817
1925
///
1818
1926
/// # Examples
1819
1927
///
@@ -1832,9 +1940,11 @@ impl Path {
1832
1940
Iter { inner : self . components ( ) }
1833
1941
}
1834
1942
1835
- /// Returns an object that implements `Display` for safely printing paths
1943
+ /// Returns an object that implements [ `Display`] for safely printing paths
1836
1944
/// that may contain non-Unicode data.
1837
1945
///
1946
+ /// [`Display`]: ../fmt/trait.Display.html
1947
+ ///
1838
1948
/// # Examples
1839
1949
///
1840
1950
/// ```
@@ -1896,11 +2006,13 @@ impl Path {
1896
2006
1897
2007
/// Returns an iterator over the entries within a directory.
1898
2008
///
1899
- /// The iterator will yield instances of `io::Result< DirEntry>`. New errors may
1900
- /// be encountered after an iterator is initially constructed.
2009
+ /// The iterator will yield instances of [ `io::Result`]`<`[` DirEntry`]` >`. New
2010
+ /// errors may be encountered after an iterator is initially constructed.
1901
2011
///
1902
2012
/// This is an alias to [`fs::read_dir`].
1903
2013
///
2014
+ /// [`io::Result`]: ../io/type.Result.html
2015
+ /// [`DirEntry`]: ../fs/struct.DirEntry.html
1904
2016
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
1905
2017
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1906
2018
pub fn read_dir ( & self ) -> io:: Result < fs:: ReadDir > {
0 commit comments