Skip to content

Commit 97b7813

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

File tree

2 files changed

+148
-33
lines changed

2 files changed

+148
-33
lines changed

src/libstd/fs.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,11 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
15111511

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

src/libstd/path.rs

Lines changed: 143 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -992,11 +992,16 @@ impl<'a> cmp::Ord for Components<'a> {
992992
// Basic types and traits
993993
////////////////////////////////////////////////////////////////////////////////
994994

995-
/// An owned, mutable path (akin to `String`).
995+
/// An owned, mutable path (akin to [`String`]).
996996
///
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
10001005
///
10011006
/// More details about the overall approach can be found in
10021007
/// the module documentation.
@@ -1023,12 +1028,31 @@ impl PathBuf {
10231028
}
10241029

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

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+
/// ```
10321056
#[stable(feature = "rust1", since = "1.0.0")]
10331057
pub fn as_path(&self) -> &Path {
10341058
self
@@ -1093,10 +1117,26 @@ impl PathBuf {
10931117
self.inner.push(path);
10941118
}
10951119

1096-
/// Truncate `self` to `self.parent()`.
1120+
/// Truncate `self` to [`self.parent()`].
10971121
///
1098-
/// Returns false and does nothing if `self.file_name()` is `None`.
1122+
/// Returns false and does nothing if [`self.file_name()`] is `None`.
10991123
/// 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+
/// ```
11001140
#[stable(feature = "rust1", since = "1.0.0")]
11011141
pub fn pop(&mut self) -> bool {
11021142
match self.parent().map(|p| p.as_u8_slice().len()) {
@@ -1108,11 +1148,13 @@ impl PathBuf {
11081148
}
11091149
}
11101150

1111-
/// Updates `self.file_name()` to `file_name`.
1151+
/// Updates [`self.file_name()`] to `file_name`.
11121152
///
11131153
/// If `self.file_name()` was `None`, this is equivalent to pushing
11141154
/// `file_name`.
11151155
///
1156+
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1157+
///
11161158
/// # Examples
11171159
///
11181160
/// ```
@@ -1139,12 +1181,29 @@ impl PathBuf {
11391181
self.push(file_name);
11401182
}
11411183

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};
11431198
///
1144-
/// If `self.file_name()` is `None`, does nothing and returns `false`.
1199+
/// let mut p = PathBuf::from("/feel/the");
11451200
///
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+
/// ```
11481207
#[stable(feature = "rust1", since = "1.0.0")]
11491208
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
11501209
self._set_extension(extension.as_ref())
@@ -1169,7 +1228,18 @@ impl PathBuf {
11691228
true
11701229
}
11711230

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+
/// ```
11731243
#[stable(feature = "rust1", since = "1.0.0")]
11741244
pub fn into_os_string(self) -> OsString {
11751245
self.inner
@@ -1307,7 +1377,7 @@ impl Into<OsString> for PathBuf {
13071377
}
13081378
}
13091379

1310-
/// A slice of a path (akin to `str`).
1380+
/// A slice of a path (akin to [`str`]).
13111381
///
13121382
/// This type supports a number of operations for inspecting a path, including
13131383
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1316,7 +1386,10 @@ impl Into<OsString> for PathBuf {
13161386
/// the module documentation.
13171387
///
13181388
/// 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
13201393
///
13211394
/// # Examples
13221395
///
@@ -1378,7 +1451,9 @@ impl Path {
13781451
unsafe { mem::transmute(s.as_ref()) }
13791452
}
13801453

1381-
/// Yields the underlying `OsStr` slice.
1454+
/// Yields the underlying [`OsStr`] slice.
1455+
///
1456+
/// [`OsStr`]: ../ffi/struct.OsStr.html
13821457
///
13831458
/// # Examples
13841459
///
@@ -1393,10 +1468,12 @@ impl Path {
13931468
&self.inner
13941469
}
13951470

1396-
/// Yields a `&str` slice if the `Path` is valid unicode.
1471+
/// Yields a [`&str`] slice if the `Path` is valid unicode.
13971472
///
13981473
/// This conversion may entail doing a check for UTF-8 validity.
13991474
///
1475+
/// [`&str`]: ../primitive.str.html
1476+
///
14001477
/// # Examples
14011478
///
14021479
/// ```
@@ -1410,10 +1487,12 @@ impl Path {
14101487
self.inner.to_str()
14111488
}
14121489

1413-
/// Converts a `Path` to a `Cow<str>`.
1490+
/// Converts a `Path` to a [`Cow<str>`].
14141491
///
14151492
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
14161493
///
1494+
/// [`Cow<str>`]: ../borrow/enum.Cow.html
1495+
///
14171496
/// # Examples
14181497
///
14191498
/// ```
@@ -1427,7 +1506,9 @@ impl Path {
14271506
self.inner.to_string_lossy()
14281507
}
14291508

1430-
/// Converts a `Path` to an owned `PathBuf`.
1509+
/// Converts a `Path` to an owned [`PathBuf`].
1510+
///
1511+
/// [`PathBuf`]: struct.PathBuf.html
14311512
///
14321513
/// # Examples
14331514
///
@@ -1575,6 +1656,18 @@ impl Path {
15751656
///
15761657
/// If `base` is not a prefix of `self` (i.e. `starts_with`
15771658
/// 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+
/// ```
15781671
#[stable(since = "1.7.0", feature = "path_strip_prefix")]
15791672
pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P)
15801673
-> Result<&'a Path, StripPrefixError>
@@ -1636,7 +1729,9 @@ impl Path {
16361729
iter_after(self.components().rev(), child.components().rev()).is_some()
16371730
}
16381731

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
16401735
///
16411736
/// The stem is:
16421737
///
@@ -1659,7 +1754,9 @@ impl Path {
16591754
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after))
16601755
}
16611756

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
16631760
///
16641761
/// The extension is:
16651762
///
@@ -1682,9 +1779,12 @@ impl Path {
16821779
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.and(after))
16831780
}
16841781

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.
16861785
///
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
16881788
///
16891789
/// # Examples
16901790
///
@@ -1704,9 +1804,12 @@ impl Path {
17041804
buf
17051805
}
17061806

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.
17081808
///
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
17101813
///
17111814
/// # Examples
17121815
///
@@ -1727,9 +1830,12 @@ impl Path {
17271830
buf
17281831
}
17291832

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.
17311836
///
1732-
/// See `PathBuf::set_extension` for more details.
1837+
/// [`PathBuf`]: struct.PathBuf.html
1838+
/// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
17331839
///
17341840
/// # Examples
17351841
///
@@ -1777,7 +1883,9 @@ impl Path {
17771883
}
17781884
}
17791885

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
17811889
///
17821890
/// # Examples
17831891
///
@@ -1796,9 +1904,11 @@ impl Path {
17961904
Iter { inner: self.components() }
17971905
}
17981906

1799-
/// Returns an object that implements `Display` for safely printing paths
1907+
/// Returns an object that implements [`Display`] for safely printing paths
18001908
/// that may contain non-Unicode data.
18011909
///
1910+
/// [`Display`]: fmt/trait.Display.html
1911+
///
18021912
/// # Examples
18031913
///
18041914
/// ```
@@ -1860,11 +1970,13 @@ impl Path {
18601970

18611971
/// Returns an iterator over the entries within a directory.
18621972
///
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.
18651975
///
18661976
/// This is an alias to [`fs::read_dir`].
18671977
///
1978+
/// [`io::Result<`]: ../io/type.Result.html
1979+
/// [`DirEntry`]: ../fs/struct.DirEntry.html
18681980
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
18691981
#[stable(feature = "path_ext", since = "1.5.0")]
18701982
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {

0 commit comments

Comments
 (0)