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