@@ -992,11 +992,16 @@ impl<'a> cmp::Ord for Components<'a> {
992
992
// Basic types and traits
993
993
////////////////////////////////////////////////////////////////////////////////
994
994
995
- /// An owned, mutable path (akin to `String`).
995
+ /// An owned, mutable path (akin to [ `String`] ).
996
996
///
997
- /// This type provides methods like `push` and `set_extension` that mutate the
998
- /// path in place. It also implements `Deref` to `Path`, meaning that all
999
- /// methods on `Path` slices are available on `PathBuf` values as well.
997
+ /// This type provides methods like [`push`] and [`set_extension`] that mutate
998
+ /// the path in place. It also implements [`Deref`] to [`Path`], meaning that
999
+ /// all methods on [`Path`] slices are available on `PathBuf` values as well.
1000
+ ///
1001
+ /// [`String`]: ../string/struct.String.html
1002
+ /// [`Path`]: struct.Path.html
1003
+ /// [`push`]: struct.PathBuf.html#method.push
1004
+ /// [`set_extension`]: struct.PathBuf.html#method.set_extension
1000
1005
///
1001
1006
/// More details about the overall approach can be found in
1002
1007
/// the module documentation.
@@ -1023,12 +1028,31 @@ impl PathBuf {
1023
1028
}
1024
1029
1025
1030
/// Allocates an empty `PathBuf`.
1031
+ ///
1032
+ /// # Examples
1033
+ ///
1034
+ /// ```
1035
+ /// use std::path::PathBuf;
1036
+ ///
1037
+ /// let path = PathBuf::new();
1038
+ /// ```
1026
1039
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1027
1040
pub fn new ( ) -> PathBuf {
1028
1041
PathBuf { inner : OsString :: new ( ) }
1029
1042
}
1030
1043
1031
- /// Coerces to a `Path` slice.
1044
+ /// Coerces to a [`Path`] slice.
1045
+ ///
1046
+ /// [`Path`]: struct.Path.html
1047
+ ///
1048
+ /// # Examples
1049
+ ///
1050
+ /// ```
1051
+ /// use std::path::{Path, PathBuf};
1052
+ ///
1053
+ /// let p = PathBuf::from("/test");
1054
+ /// assert_eq!(Path::new("/test"), p.as_path());
1055
+ /// ```
1032
1056
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1033
1057
pub fn as_path ( & self ) -> & Path {
1034
1058
self
@@ -1093,10 +1117,26 @@ impl PathBuf {
1093
1117
self . inner . push ( path) ;
1094
1118
}
1095
1119
1096
- /// Truncate `self` to `self.parent()`.
1120
+ /// Truncate `self` to [ `self.parent()`] .
1097
1121
///
1098
- /// Returns false and does nothing if `self.file_name()` is `None`.
1122
+ /// Returns false and does nothing if [ `self.file_name()`] is `None`.
1099
1123
/// Otherwise, returns `true`.
1124
+ ///
1125
+ /// [`self.parent()`]: struct.PathBuf.html#method.parent
1126
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1127
+ ///
1128
+ /// # Examples
1129
+ ///
1130
+ /// ```
1131
+ /// use std::path::{Path, PathBuf};
1132
+ ///
1133
+ /// let mut p = PathBuf::from("/test/test.rs");
1134
+ ///
1135
+ /// p.pop();
1136
+ /// assert_eq!(Path::new("/test"), p.as_path());
1137
+ /// p.pop();
1138
+ /// assert_eq!(Path::new("/"), p.as_path());
1139
+ /// ```
1100
1140
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1101
1141
pub fn pop ( & mut self ) -> bool {
1102
1142
match self . parent ( ) . map ( |p| p. as_u8_slice ( ) . len ( ) ) {
@@ -1108,11 +1148,13 @@ impl PathBuf {
1108
1148
}
1109
1149
}
1110
1150
1111
- /// Updates `self.file_name()` to `file_name`.
1151
+ /// Updates [ `self.file_name()`] to `file_name`.
1112
1152
///
1113
1153
/// If `self.file_name()` was `None`, this is equivalent to pushing
1114
1154
/// `file_name`.
1115
1155
///
1156
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1157
+ ///
1116
1158
/// # Examples
1117
1159
///
1118
1160
/// ```
@@ -1139,12 +1181,29 @@ impl PathBuf {
1139
1181
self . push ( file_name) ;
1140
1182
}
1141
1183
1142
- /// Updates `self.extension()` to `extension`.
1184
+ /// Updates [`self.extension()`] to `extension`.
1185
+ ///
1186
+ /// If [`self.file_name()`] is `None`, does nothing and returns `false`.
1187
+ ///
1188
+ /// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
1189
+ /// extension is added; otherwise it is replaced.
1190
+ ///
1191
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1192
+ /// [`self.extension()`]: struct.PathBuf.html#method.extension
1193
+ ///
1194
+ /// # Examples
1195
+ ///
1196
+ /// ```
1197
+ /// use std::path::{Path, PathBuf};
1143
1198
///
1144
- /// If `self.file_name()` is `None`, does nothing and returns `false`.
1199
+ /// let mut p = PathBuf::from("/feel/the");
1145
1200
///
1146
- /// Otherwise, returns `true`; if `self.extension()` is `None`, the extension
1147
- /// is added; otherwise it is replaced.
1201
+ /// p.set_extension("force");
1202
+ /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
1203
+ ///
1204
+ /// p.set_extension("dark_side");
1205
+ /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
1206
+ /// ```
1148
1207
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1149
1208
pub fn set_extension < S : AsRef < OsStr > > ( & mut self , extension : S ) -> bool {
1150
1209
self . _set_extension ( extension. as_ref ( ) )
@@ -1169,7 +1228,18 @@ impl PathBuf {
1169
1228
true
1170
1229
}
1171
1230
1172
- /// Consumes the `PathBuf`, yielding its internal `OsString` storage.
1231
+ /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
1232
+ ///
1233
+ /// [`OsString`]: ../ffi/struct.OsString.html
1234
+ ///
1235
+ /// # Examples
1236
+ ///
1237
+ /// ```
1238
+ /// use std::path::PathBuf;
1239
+ ///
1240
+ /// let p = PathBuf::from("/the/head");
1241
+ /// let os_str = p.into_os_string();
1242
+ /// ```
1173
1243
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1174
1244
pub fn into_os_string ( self ) -> OsString {
1175
1245
self . inner
@@ -1307,7 +1377,7 @@ impl Into<OsString> for PathBuf {
1307
1377
}
1308
1378
}
1309
1379
1310
- /// A slice of a path (akin to `str`).
1380
+ /// A slice of a path (akin to [ `str`] ).
1311
1381
///
1312
1382
/// This type supports a number of operations for inspecting a path, including
1313
1383
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1316,7 +1386,10 @@ impl Into<OsString> for PathBuf {
1316
1386
/// the module documentation.
1317
1387
///
1318
1388
/// This is an *unsized* type, meaning that it must always be used behind a
1319
- /// pointer like `&` or `Box`.
1389
+ /// pointer like `&` or [`Box`].
1390
+ ///
1391
+ /// [`str`]: ../primitive.str.html
1392
+ /// [`Box`]: ../boxed/struct.Box.html
1320
1393
///
1321
1394
/// # Examples
1322
1395
///
@@ -1378,7 +1451,9 @@ impl Path {
1378
1451
unsafe { mem:: transmute ( s. as_ref ( ) ) }
1379
1452
}
1380
1453
1381
- /// Yields the underlying `OsStr` slice.
1454
+ /// Yields the underlying [`OsStr`] slice.
1455
+ ///
1456
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
1382
1457
///
1383
1458
/// # Examples
1384
1459
///
@@ -1393,10 +1468,12 @@ impl Path {
1393
1468
& self . inner
1394
1469
}
1395
1470
1396
- /// Yields a `&str` slice if the `Path` is valid unicode.
1471
+ /// Yields a [ `&str`] slice if the `Path` is valid unicode.
1397
1472
///
1398
1473
/// This conversion may entail doing a check for UTF-8 validity.
1399
1474
///
1475
+ /// [`&str`]: ../primitive.str.html
1476
+ ///
1400
1477
/// # Examples
1401
1478
///
1402
1479
/// ```
@@ -1410,10 +1487,12 @@ impl Path {
1410
1487
self . inner . to_str ( )
1411
1488
}
1412
1489
1413
- /// Converts a `Path` to a `Cow<str>`.
1490
+ /// Converts a `Path` to a [ `Cow<str>`] .
1414
1491
///
1415
1492
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
1416
1493
///
1494
+ /// [`Cow<str>`]: ../borrow/enum.Cow.html
1495
+ ///
1417
1496
/// # Examples
1418
1497
///
1419
1498
/// ```
@@ -1427,7 +1506,9 @@ impl Path {
1427
1506
self . inner . to_string_lossy ( )
1428
1507
}
1429
1508
1430
- /// Converts a `Path` to an owned `PathBuf`.
1509
+ /// Converts a `Path` to an owned [`PathBuf`].
1510
+ ///
1511
+ /// [`PathBuf`]: struct.PathBuf.html
1431
1512
///
1432
1513
/// # Examples
1433
1514
///
@@ -1575,6 +1656,18 @@ impl Path {
1575
1656
///
1576
1657
/// If `base` is not a prefix of `self` (i.e. `starts_with`
1577
1658
/// returns `false`), returns `Err`.
1659
+ ///
1660
+ /// # Examples
1661
+ ///
1662
+ /// ```
1663
+ /// use std::path::Path;
1664
+ ///
1665
+ /// let path = Path::new("/test/haha/foo.txt");
1666
+ ///
1667
+ /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
1668
+ /// assert_eq!(path.strip_prefix("test").is_ok(), false);
1669
+ /// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
1670
+ /// ```
1578
1671
#[ stable( since = "1.7.0" , feature = "path_strip_prefix" ) ]
1579
1672
pub fn strip_prefix < ' a , P : ?Sized > ( & ' a self , base : & ' a P )
1580
1673
-> Result < & ' a Path , StripPrefixError >
@@ -1636,7 +1729,9 @@ impl Path {
1636
1729
iter_after ( self . components ( ) . rev ( ) , child. components ( ) . rev ( ) ) . is_some ( )
1637
1730
}
1638
1731
1639
- /// Extracts the stem (non-extension) portion of `self.file_name()`.
1732
+ /// Extracts the stem (non-extension) portion of [`self.file_name()`].
1733
+ ///
1734
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1640
1735
///
1641
1736
/// The stem is:
1642
1737
///
@@ -1659,7 +1754,9 @@ impl Path {
1659
1754
self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, after) | before. or ( after) )
1660
1755
}
1661
1756
1662
- /// Extracts the extension of `self.file_name()`, if possible.
1757
+ /// Extracts the extension of [`self.file_name()`], if possible.
1758
+ ///
1759
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1663
1760
///
1664
1761
/// The extension is:
1665
1762
///
@@ -1682,9 +1779,12 @@ impl Path {
1682
1779
self . file_name ( ) . map ( split_file_at_dot) . and_then ( |( before, after) | before. and ( after) )
1683
1780
}
1684
1781
1685
- /// Creates an owned `PathBuf` with `path` adjoined to `self`.
1782
+ /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
1783
+ ///
1784
+ /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
1686
1785
///
1687
- /// See `PathBuf::push` for more details on what it means to adjoin a path.
1786
+ /// [`PathBuf`]: struct.PathBuf.html
1787
+ /// [`PathBuf::push`]: struct.PathBuf.html#method.push
1688
1788
///
1689
1789
/// # Examples
1690
1790
///
@@ -1704,9 +1804,12 @@ impl Path {
1704
1804
buf
1705
1805
}
1706
1806
1707
- /// Creates an owned `PathBuf` like `self` but with the given file name.
1807
+ /// Creates an owned [ `PathBuf`] like `self` but with the given file name.
1708
1808
///
1709
- /// See `PathBuf::set_file_name` for more details.
1809
+ /// See [`PathBuf::set_file_name`] for more details.
1810
+ ///
1811
+ /// [`PathBuf`]: struct.PathBuf.html
1812
+ /// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
1710
1813
///
1711
1814
/// # Examples
1712
1815
///
@@ -1727,9 +1830,12 @@ impl Path {
1727
1830
buf
1728
1831
}
1729
1832
1730
- /// Creates an owned `PathBuf` like `self` but with the given extension.
1833
+ /// Creates an owned [`PathBuf`] like `self` but with the given extension.
1834
+ ///
1835
+ /// See [`PathBuf::set_extension`] for more details.
1731
1836
///
1732
- /// See `PathBuf::set_extension` for more details.
1837
+ /// [`PathBuf`]: struct.PathBuf.html
1838
+ /// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
1733
1839
///
1734
1840
/// # Examples
1735
1841
///
@@ -1777,7 +1883,9 @@ impl Path {
1777
1883
}
1778
1884
}
1779
1885
1780
- /// Produce an iterator over the path's components viewed as `OsStr` slices.
1886
+ /// Produce an iterator over the path's components viewed as [`OsStr`] slices.
1887
+ ///
1888
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
1781
1889
///
1782
1890
/// # Examples
1783
1891
///
@@ -1796,9 +1904,11 @@ impl Path {
1796
1904
Iter { inner : self . components ( ) }
1797
1905
}
1798
1906
1799
- /// Returns an object that implements `Display` for safely printing paths
1907
+ /// Returns an object that implements [ `Display`] for safely printing paths
1800
1908
/// that may contain non-Unicode data.
1801
1909
///
1910
+ /// [`Display`]: fmt/trait.Display.html
1911
+ ///
1802
1912
/// # Examples
1803
1913
///
1804
1914
/// ```
@@ -1860,11 +1970,13 @@ impl Path {
1860
1970
1861
1971
/// Returns an iterator over the entries within a directory.
1862
1972
///
1863
- /// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
1864
- /// be encountered after an iterator is initially constructed.
1973
+ /// The iterator will yield instances of [ `io::Result<`][` DirEntry`]` >`. New
1974
+ /// errors may be encountered after an iterator is initially constructed.
1865
1975
///
1866
1976
/// This is an alias to [`fs::read_dir`].
1867
1977
///
1978
+ /// [`io::Result<`]: ../io/type.Result.html
1979
+ /// [`DirEntry`]: ../fs/struct.DirEntry.html
1868
1980
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
1869
1981
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1870
1982
pub fn read_dir ( & self ) -> io:: Result < fs:: ReadDir > {
0 commit comments