Skip to content

Commit d16fb03

Browse files
authored
Rollup merge of rust-lang#35786 - GuillaumeGomez:paths_doc, r=steveklabnik
Improve Path and PathBuf docs r? @steveklabnik
2 parents 34ce63c + a377adb commit d16fb03

File tree

2 files changed

+148
-33
lines changed

2 files changed

+148
-33
lines changed

src/libstd/fs.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1510,8 +1510,11 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
15101510

15111511
/// Returns an iterator over the entries within a directory.
15121512
///
1513-
/// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
1514-
/// be encountered after an iterator is initially constructed.
1513+
/// The iterator will yield instances of [`io::Result<`][`DirEntry`]`>`.
1514+
/// New errors may be encountered after an iterator is initially constructed.
1515+
///
1516+
/// [`io::Result<`]: ../io/type.Result.html
1517+
/// [`DirEntry`]: struct.DirEntry.html
15151518
///
15161519
/// # Platform-specific behavior
15171520
///

src/libstd/path.rs

+143-31
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,16 @@ impl<'a> cmp::Ord for Components<'a> {
990990
// Basic types and traits
991991
////////////////////////////////////////////////////////////////////////////////
992992

993-
/// An owned, mutable path (akin to `String`).
993+
/// An owned, mutable path (akin to [`String`]).
994994
///
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
9981003
///
9991004
/// More details about the overall approach can be found in
10001005
/// the module documentation.
@@ -1021,12 +1026,31 @@ impl PathBuf {
10211026
}
10221027

10231028
/// Allocates an empty `PathBuf`.
1029+
///
1030+
/// # Examples
1031+
///
1032+
/// ```
1033+
/// use std::path::PathBuf;
1034+
///
1035+
/// let path = PathBuf::new();
1036+
/// ```
10241037
#[stable(feature = "rust1", since = "1.0.0")]
10251038
pub fn new() -> PathBuf {
10261039
PathBuf { inner: OsString::new() }
10271040
}
10281041

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+
/// ```
10301054
#[stable(feature = "rust1", since = "1.0.0")]
10311055
pub fn as_path(&self) -> &Path {
10321056
self
@@ -1091,10 +1115,26 @@ impl PathBuf {
10911115
self.inner.push(path);
10921116
}
10931117

1094-
/// Truncate `self` to `self.parent()`.
1118+
/// Truncate `self` to [`self.parent()`].
10951119
///
1096-
/// Returns false and does nothing if `self.file_name()` is `None`.
1120+
/// Returns false and does nothing if [`self.file_name()`] is `None`.
10971121
/// 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+
/// ```
10981138
#[stable(feature = "rust1", since = "1.0.0")]
10991139
pub fn pop(&mut self) -> bool {
11001140
match self.parent().map(|p| p.as_u8_slice().len()) {
@@ -1106,11 +1146,13 @@ impl PathBuf {
11061146
}
11071147
}
11081148

1109-
/// Updates `self.file_name()` to `file_name`.
1149+
/// Updates [`self.file_name()`] to `file_name`.
11101150
///
11111151
/// If `self.file_name()` was `None`, this is equivalent to pushing
11121152
/// `file_name`.
11131153
///
1154+
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1155+
///
11141156
/// # Examples
11151157
///
11161158
/// ```
@@ -1137,12 +1179,29 @@ impl PathBuf {
11371179
self.push(file_name);
11381180
}
11391181

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};
11411196
///
1142-
/// If `self.file_name()` is `None`, does nothing and returns `false`.
1197+
/// let mut p = PathBuf::from("/feel/the");
11431198
///
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+
/// ```
11461205
#[stable(feature = "rust1", since = "1.0.0")]
11471206
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
11481207
self._set_extension(extension.as_ref())
@@ -1167,7 +1226,18 @@ impl PathBuf {
11671226
true
11681227
}
11691228

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+
/// ```
11711241
#[stable(feature = "rust1", since = "1.0.0")]
11721242
pub fn into_os_string(self) -> OsString {
11731243
self.inner
@@ -1305,7 +1375,7 @@ impl Into<OsString> for PathBuf {
13051375
}
13061376
}
13071377

1308-
/// A slice of a path (akin to `str`).
1378+
/// A slice of a path (akin to [`str`]).
13091379
///
13101380
/// This type supports a number of operations for inspecting a path, including
13111381
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1314,7 +1384,10 @@ impl Into<OsString> for PathBuf {
13141384
/// the module documentation.
13151385
///
13161386
/// 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
13181391
///
13191392
/// # Examples
13201393
///
@@ -1376,7 +1449,9 @@ impl Path {
13761449
unsafe { mem::transmute(s.as_ref()) }
13771450
}
13781451

1379-
/// Yields the underlying `OsStr` slice.
1452+
/// Yields the underlying [`OsStr`] slice.
1453+
///
1454+
/// [`OsStr`]: ../ffi/struct.OsStr.html
13801455
///
13811456
/// # Examples
13821457
///
@@ -1391,10 +1466,12 @@ impl Path {
13911466
&self.inner
13921467
}
13931468

1394-
/// Yields a `&str` slice if the `Path` is valid unicode.
1469+
/// Yields a [`&str`] slice if the `Path` is valid unicode.
13951470
///
13961471
/// This conversion may entail doing a check for UTF-8 validity.
13971472
///
1473+
/// [`&str`]: ../primitive.str.html
1474+
///
13981475
/// # Examples
13991476
///
14001477
/// ```
@@ -1408,10 +1485,12 @@ impl Path {
14081485
self.inner.to_str()
14091486
}
14101487

1411-
/// Converts a `Path` to a `Cow<str>`.
1488+
/// Converts a `Path` to a [`Cow<str>`].
14121489
///
14131490
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
14141491
///
1492+
/// [`Cow<str>`]: ../borrow/enum.Cow.html
1493+
///
14151494
/// # Examples
14161495
///
14171496
/// ```
@@ -1425,7 +1504,9 @@ impl Path {
14251504
self.inner.to_string_lossy()
14261505
}
14271506

1428-
/// Converts a `Path` to an owned `PathBuf`.
1507+
/// Converts a `Path` to an owned [`PathBuf`].
1508+
///
1509+
/// [`PathBuf`]: struct.PathBuf.html
14291510
///
14301511
/// # Examples
14311512
///
@@ -1573,6 +1654,18 @@ impl Path {
15731654
///
15741655
/// If `base` is not a prefix of `self` (i.e. `starts_with`
15751656
/// 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+
/// ```
15761669
#[stable(since = "1.7.0", feature = "path_strip_prefix")]
15771670
pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P)
15781671
-> Result<&'a Path, StripPrefixError>
@@ -1634,7 +1727,9 @@ impl Path {
16341727
iter_after(self.components().rev(), child.components().rev()).is_some()
16351728
}
16361729

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
16381733
///
16391734
/// The stem is:
16401735
///
@@ -1657,7 +1752,9 @@ impl Path {
16571752
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after))
16581753
}
16591754

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
16611758
///
16621759
/// The extension is:
16631760
///
@@ -1680,9 +1777,12 @@ impl Path {
16801777
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.and(after))
16811778
}
16821779

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.
16841783
///
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
16861786
///
16871787
/// # Examples
16881788
///
@@ -1702,9 +1802,12 @@ impl Path {
17021802
buf
17031803
}
17041804

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.
17061806
///
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
17081811
///
17091812
/// # Examples
17101813
///
@@ -1725,9 +1828,12 @@ impl Path {
17251828
buf
17261829
}
17271830

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.
17291834
///
1730-
/// See `PathBuf::set_extension` for more details.
1835+
/// [`PathBuf`]: struct.PathBuf.html
1836+
/// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
17311837
///
17321838
/// # Examples
17331839
///
@@ -1775,7 +1881,9 @@ impl Path {
17751881
}
17761882
}
17771883

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
17791887
///
17801888
/// # Examples
17811889
///
@@ -1794,9 +1902,11 @@ impl Path {
17941902
Iter { inner: self.components() }
17951903
}
17961904

1797-
/// Returns an object that implements `Display` for safely printing paths
1905+
/// Returns an object that implements [`Display`] for safely printing paths
17981906
/// that may contain non-Unicode data.
17991907
///
1908+
/// [`Display`]: fmt/trait.Display.html
1909+
///
18001910
/// # Examples
18011911
///
18021912
/// ```
@@ -1858,11 +1968,13 @@ impl Path {
18581968

18591969
/// Returns an iterator over the entries within a directory.
18601970
///
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.
18631973
///
18641974
/// This is an alias to [`fs::read_dir`].
18651975
///
1976+
/// [`io::Result<`]: ../io/type.Result.html
1977+
/// [`DirEntry`]: ../fs/struct.DirEntry.html
18661978
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
18671979
#[stable(feature = "path_ext", since = "1.5.0")]
18681980
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {

0 commit comments

Comments
 (0)