Skip to content

Commit 4bc5bcd

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35786 - GuillaumeGomez:paths_doc, r=steveklabnik
Improve Path and PathBuf docs r? @steveklabnik
2 parents c75fd78 + 96e3103 commit 4bc5bcd

File tree

2 files changed

+149
-34
lines changed

2 files changed

+149
-34
lines changed

src/libstd/fs.rs

Lines changed: 5 additions & 2 deletions
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

Lines changed: 144 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,16 @@ impl<'a> cmp::Ord for Components<'a> {
10281028
// Basic types and traits
10291029
////////////////////////////////////////////////////////////////////////////////
10301030

1031-
/// An owned, mutable path (akin to `String`).
1031+
/// An owned, mutable path (akin to [`String`]).
10321032
///
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
10361041
///
10371042
/// More details about the overall approach can be found in
10381043
/// the module documentation.
@@ -1059,12 +1064,31 @@ impl PathBuf {
10591064
}
10601065

10611066
/// Allocates an empty `PathBuf`.
1067+
///
1068+
/// # Examples
1069+
///
1070+
/// ```
1071+
/// use std::path::PathBuf;
1072+
///
1073+
/// let path = PathBuf::new();
1074+
/// ```
10621075
#[stable(feature = "rust1", since = "1.0.0")]
10631076
pub fn new() -> PathBuf {
10641077
PathBuf { inner: OsString::new() }
10651078
}
10661079

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+
/// ```
10681092
#[stable(feature = "rust1", since = "1.0.0")]
10691093
pub fn as_path(&self) -> &Path {
10701094
self
@@ -1129,10 +1153,26 @@ impl PathBuf {
11291153
self.inner.push(path);
11301154
}
11311155

1132-
/// Truncate `self` to `self.parent()`.
1156+
/// Truncate `self` to [`self.parent()`].
11331157
///
1134-
/// Returns false and does nothing if `self.file_name()` is `None`.
1158+
/// Returns false and does nothing if [`self.file_name()`] is `None`.
11351159
/// 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+
/// ```
11361176
#[stable(feature = "rust1", since = "1.0.0")]
11371177
pub fn pop(&mut self) -> bool {
11381178
match self.parent().map(|p| p.as_u8_slice().len()) {
@@ -1144,11 +1184,13 @@ impl PathBuf {
11441184
}
11451185
}
11461186

1147-
/// Updates `self.file_name()` to `file_name`.
1187+
/// Updates [`self.file_name()`] to `file_name`.
11481188
///
1149-
/// If `self.file_name()` was `None`, this is equivalent to pushing
1189+
/// If [`self.file_name()`] was `None`, this is equivalent to pushing
11501190
/// `file_name`.
11511191
///
1192+
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1193+
///
11521194
/// # Examples
11531195
///
11541196
/// ```
@@ -1175,12 +1217,29 @@ impl PathBuf {
11751217
self.push(file_name);
11761218
}
11771219

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};
11791234
///
1180-
/// If `self.file_name()` is `None`, does nothing and returns `false`.
1235+
/// let mut p = PathBuf::from("/feel/the");
11811236
///
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+
/// ```
11841243
#[stable(feature = "rust1", since = "1.0.0")]
11851244
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
11861245
self._set_extension(extension.as_ref())
@@ -1205,7 +1264,18 @@ impl PathBuf {
12051264
true
12061265
}
12071266

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+
/// ```
12091279
#[stable(feature = "rust1", since = "1.0.0")]
12101280
pub fn into_os_string(self) -> OsString {
12111281
self.inner
@@ -1343,7 +1413,7 @@ impl Into<OsString> for PathBuf {
13431413
}
13441414
}
13451415

1346-
/// A slice of a path (akin to `str`).
1416+
/// A slice of a path (akin to [`str`]).
13471417
///
13481418
/// This type supports a number of operations for inspecting a path, including
13491419
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1352,7 +1422,10 @@ impl Into<OsString> for PathBuf {
13521422
/// the module documentation.
13531423
///
13541424
/// 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
13561429
///
13571430
/// # Examples
13581431
///
@@ -1414,7 +1487,9 @@ impl Path {
14141487
unsafe { mem::transmute(s.as_ref()) }
14151488
}
14161489

1417-
/// Yields the underlying `OsStr` slice.
1490+
/// Yields the underlying [`OsStr`] slice.
1491+
///
1492+
/// [`OsStr`]: ../ffi/struct.OsStr.html
14181493
///
14191494
/// # Examples
14201495
///
@@ -1429,10 +1504,12 @@ impl Path {
14291504
&self.inner
14301505
}
14311506

1432-
/// Yields a `&str` slice if the `Path` is valid unicode.
1507+
/// Yields a [`&str`] slice if the `Path` is valid unicode.
14331508
///
14341509
/// This conversion may entail doing a check for UTF-8 validity.
14351510
///
1511+
/// [`&str`]: ../primitive.str.html
1512+
///
14361513
/// # Examples
14371514
///
14381515
/// ```
@@ -1446,10 +1523,12 @@ impl Path {
14461523
self.inner.to_str()
14471524
}
14481525

1449-
/// Converts a `Path` to a `Cow<str>`.
1526+
/// Converts a `Path` to a [`Cow<str>`].
14501527
///
14511528
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
14521529
///
1530+
/// [`Cow<str>`]: ../borrow/enum.Cow.html
1531+
///
14531532
/// # Examples
14541533
///
14551534
/// ```
@@ -1463,7 +1542,9 @@ impl Path {
14631542
self.inner.to_string_lossy()
14641543
}
14651544

1466-
/// Converts a `Path` to an owned `PathBuf`.
1545+
/// Converts a `Path` to an owned [`PathBuf`].
1546+
///
1547+
/// [`PathBuf`]: struct.PathBuf.html
14671548
///
14681549
/// # Examples
14691550
///
@@ -1611,6 +1692,18 @@ impl Path {
16111692
///
16121693
/// If `base` is not a prefix of `self` (i.e. `starts_with`
16131694
/// 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+
/// ```
16141707
#[stable(since = "1.7.0", feature = "path_strip_prefix")]
16151708
pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P)
16161709
-> Result<&'a Path, StripPrefixError>
@@ -1672,7 +1765,9 @@ impl Path {
16721765
iter_after(self.components().rev(), child.components().rev()).is_some()
16731766
}
16741767

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
16761771
///
16771772
/// The stem is:
16781773
///
@@ -1695,7 +1790,9 @@ impl Path {
16951790
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after))
16961791
}
16971792

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
16991796
///
17001797
/// The extension is:
17011798
///
@@ -1718,9 +1815,12 @@ impl Path {
17181815
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.and(after))
17191816
}
17201817

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.
17221821
///
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
17241824
///
17251825
/// # Examples
17261826
///
@@ -1740,9 +1840,12 @@ impl Path {
17401840
buf
17411841
}
17421842

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.
17441844
///
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
17461849
///
17471850
/// # Examples
17481851
///
@@ -1763,9 +1866,12 @@ impl Path {
17631866
buf
17641867
}
17651868

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.
17671872
///
1768-
/// See `PathBuf::set_extension` for more details.
1873+
/// [`PathBuf`]: struct.PathBuf.html
1874+
/// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
17691875
///
17701876
/// # Examples
17711877
///
@@ -1813,7 +1919,9 @@ impl Path {
18131919
}
18141920
}
18151921

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
18171925
///
18181926
/// # Examples
18191927
///
@@ -1832,9 +1940,11 @@ impl Path {
18321940
Iter { inner: self.components() }
18331941
}
18341942

1835-
/// Returns an object that implements `Display` for safely printing paths
1943+
/// Returns an object that implements [`Display`] for safely printing paths
18361944
/// that may contain non-Unicode data.
18371945
///
1946+
/// [`Display`]: ../fmt/trait.Display.html
1947+
///
18381948
/// # Examples
18391949
///
18401950
/// ```
@@ -1896,11 +2006,13 @@ impl Path {
18962006

18972007
/// Returns an iterator over the entries within a directory.
18982008
///
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.
19012011
///
19022012
/// This is an alias to [`fs::read_dir`].
19032013
///
2014+
/// [`io::Result`]: ../io/type.Result.html
2015+
/// [`DirEntry`]: ../fs/struct.DirEntry.html
19042016
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
19052017
#[stable(feature = "path_ext", since = "1.5.0")]
19062018
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {

0 commit comments

Comments
 (0)