Skip to content

Commit 481b0f3

Browse files
Improve Path and PathBuf docs
1 parent 9d6520f commit 481b0f3

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
@@ -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`]: ../fs/struct.DirEntry.html
15161519
///
15171520
/// # Platform-specific behavior
15181521
///

src/libstd/path.rs

+143-31
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,16 @@ impl<'a> cmp::Ord for Components<'a> {
986986
// Basic types and traits
987987
////////////////////////////////////////////////////////////////////////////////
988988

989-
/// An owned, mutable path (akin to `String`).
989+
/// An owned, mutable path (akin to [`String`]).
990990
///
991-
/// This type provides methods like `push` and `set_extension` that mutate the
992-
/// path in place. It also implements `Deref` to `Path`, meaning that all
993-
/// methods on `Path` slices are available on `PathBuf` values as well.
991+
/// This type provides methods like [`push`] and [`set_extension`] that mutate
992+
/// the path in place. It also implements [`Deref`] to [`Path`], meaning that
993+
/// all methods on [`Path`] slices are available on `PathBuf` values as well.
994+
///
995+
/// [`String`]: ../string/struct.String.html
996+
/// [`Path`]: struct.Path.html
997+
/// [`push`]: struct.PathBuf.html#method.push
998+
/// [`set_extension`]: struct.PathBuf.html#method.set_extension
994999
///
9951000
/// More details about the overall approach can be found in
9961001
/// the module documentation.
@@ -1017,12 +1022,31 @@ impl PathBuf {
10171022
}
10181023

10191024
/// Allocates an empty `PathBuf`.
1025+
///
1026+
/// # Examples
1027+
///
1028+
/// ```
1029+
/// use std::path::PathBuf;
1030+
///
1031+
/// let path = PathBuf::new();
1032+
/// ```
10201033
#[stable(feature = "rust1", since = "1.0.0")]
10211034
pub fn new() -> PathBuf {
10221035
PathBuf { inner: OsString::new() }
10231036
}
10241037

1025-
/// Coerces to a `Path` slice.
1038+
/// Coerces to a [`Path`] slice.
1039+
///
1040+
/// [`Path`]: struct.Path.html
1041+
///
1042+
/// # Examples
1043+
///
1044+
/// ```
1045+
/// use std::path::{Path, PathBuf};
1046+
///
1047+
/// let p = PathBuf::from("/test");
1048+
/// assert_eq!(Path::new("/test"), p.as_path());
1049+
/// ```
10261050
#[stable(feature = "rust1", since = "1.0.0")]
10271051
pub fn as_path(&self) -> &Path {
10281052
self
@@ -1087,10 +1111,26 @@ impl PathBuf {
10871111
self.inner.push(path);
10881112
}
10891113

1090-
/// Truncate `self` to `self.parent()`.
1114+
/// Truncate `self` to [`self.parent()`].
10911115
///
1092-
/// Returns false and does nothing if `self.file_name()` is `None`.
1116+
/// Returns false and does nothing if [`self.file_name()`] is `None`.
10931117
/// Otherwise, returns `true`.
1118+
///
1119+
/// [`self.parent()`]: struct.PathBuf.html#method.parent
1120+
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1121+
///
1122+
/// # Examples
1123+
///
1124+
/// ```
1125+
/// use std::path::{Path, PathBuf};
1126+
///
1127+
/// let mut p = PathBuf::from("/test/test.rs");
1128+
///
1129+
/// p.pop();
1130+
/// assert_eq!(Path::new("/test"), p.as_path());
1131+
/// p.pop();
1132+
/// assert_eq!(Path::new("/"), p.as_path());
1133+
/// ```
10941134
#[stable(feature = "rust1", since = "1.0.0")]
10951135
pub fn pop(&mut self) -> bool {
10961136
match self.parent().map(|p| p.as_u8_slice().len()) {
@@ -1102,11 +1142,13 @@ impl PathBuf {
11021142
}
11031143
}
11041144

1105-
/// Updates `self.file_name()` to `file_name`.
1145+
/// Updates [`self.file_name()`] to `file_name`.
11061146
///
11071147
/// If `self.file_name()` was `None`, this is equivalent to pushing
11081148
/// `file_name`.
11091149
///
1150+
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1151+
///
11101152
/// # Examples
11111153
///
11121154
/// ```
@@ -1133,12 +1175,29 @@ impl PathBuf {
11331175
self.push(file_name);
11341176
}
11351177

1136-
/// Updates `self.extension()` to `extension`.
1178+
/// Updates [`self.extension()`] to `extension`.
1179+
///
1180+
/// If [`self.file_name()`] is `None`, does nothing and returns `false`.
1181+
///
1182+
/// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
1183+
/// extension is added; otherwise it is replaced.
1184+
///
1185+
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1186+
/// [`self.extension()`]: struct.PathBuf.html#method.extension
1187+
///
1188+
/// # Examples
1189+
///
1190+
/// ```
1191+
/// use std::path::{Path, PathBuf};
11371192
///
1138-
/// If `self.file_name()` is `None`, does nothing and returns `false`.
1193+
/// let mut p = PathBuf::from("/feel/the");
11391194
///
1140-
/// Otherwise, returns `true`; if `self.extension()` is `None`, the extension
1141-
/// is added; otherwise it is replaced.
1195+
/// p.set_extension("force");
1196+
/// assert_eq!(Path::new("/feel/the.force"), p.as_path());
1197+
///
1198+
/// p.set_extension("dark_side");
1199+
/// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
1200+
/// ```
11421201
#[stable(feature = "rust1", since = "1.0.0")]
11431202
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
11441203
self._set_extension(extension.as_ref())
@@ -1163,7 +1222,18 @@ impl PathBuf {
11631222
true
11641223
}
11651224

1166-
/// Consumes the `PathBuf`, yielding its internal `OsString` storage.
1225+
/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
1226+
///
1227+
/// [`OsString`]: ../ffi/struct.OsString.html
1228+
///
1229+
/// # Examples
1230+
///
1231+
/// ```
1232+
/// use std::path::PathBuf;
1233+
///
1234+
/// let p = PathBuf::from("/the/head");
1235+
/// let os_str = p.into_os_string();
1236+
/// ```
11671237
#[stable(feature = "rust1", since = "1.0.0")]
11681238
pub fn into_os_string(self) -> OsString {
11691239
self.inner
@@ -1301,7 +1371,7 @@ impl Into<OsString> for PathBuf {
13011371
}
13021372
}
13031373

1304-
/// A slice of a path (akin to `str`).
1374+
/// A slice of a path (akin to [`str`]).
13051375
///
13061376
/// This type supports a number of operations for inspecting a path, including
13071377
/// breaking the path into its components (separated by `/` or `\`, depending on
@@ -1310,7 +1380,10 @@ impl Into<OsString> for PathBuf {
13101380
/// the module documentation.
13111381
///
13121382
/// This is an *unsized* type, meaning that it must always be used behind a
1313-
/// pointer like `&` or `Box`.
1383+
/// pointer like `&` or [`Box`].
1384+
///
1385+
/// [`str`]: ../primitive.str.html
1386+
/// [`Box`]: ../boxed/struct.Box.html
13141387
///
13151388
/// # Examples
13161389
///
@@ -1372,7 +1445,9 @@ impl Path {
13721445
unsafe { mem::transmute(s.as_ref()) }
13731446
}
13741447

1375-
/// Yields the underlying `OsStr` slice.
1448+
/// Yields the underlying [`OsStr`] slice.
1449+
///
1450+
/// [`OsStr`]: ../ffi/struct.OsStr.html
13761451
///
13771452
/// # Examples
13781453
///
@@ -1387,10 +1462,12 @@ impl Path {
13871462
&self.inner
13881463
}
13891464

1390-
/// Yields a `&str` slice if the `Path` is valid unicode.
1465+
/// Yields a [`&str`] slice if the `Path` is valid unicode.
13911466
///
13921467
/// This conversion may entail doing a check for UTF-8 validity.
13931468
///
1469+
/// [`&str`]: ../primitive.str.html
1470+
///
13941471
/// # Examples
13951472
///
13961473
/// ```
@@ -1404,10 +1481,12 @@ impl Path {
14041481
self.inner.to_str()
14051482
}
14061483

1407-
/// Converts a `Path` to a `Cow<str>`.
1484+
/// Converts a `Path` to a [`Cow<str>`].
14081485
///
14091486
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
14101487
///
1488+
/// [`Cow<str>`]: ../borrow/enum.Cow.html
1489+
///
14111490
/// # Examples
14121491
///
14131492
/// ```
@@ -1421,7 +1500,9 @@ impl Path {
14211500
self.inner.to_string_lossy()
14221501
}
14231502

1424-
/// Converts a `Path` to an owned `PathBuf`.
1503+
/// Converts a `Path` to an owned [`PathBuf`].
1504+
///
1505+
/// [`PathBuf`]: struct.PathBuf.html
14251506
///
14261507
/// # Examples
14271508
///
@@ -1569,6 +1650,18 @@ impl Path {
15691650
///
15701651
/// If `base` is not a prefix of `self` (i.e. `starts_with`
15711652
/// returns `false`), returns `Err`.
1653+
///
1654+
/// # Examples
1655+
///
1656+
/// ```
1657+
/// use std::path::Path;
1658+
///
1659+
/// let path = Path::new("/test/haha/foo.txt");
1660+
///
1661+
/// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
1662+
/// assert_eq!(path.strip_prefix("test").is_ok(), false);
1663+
/// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
1664+
/// ```
15721665
#[stable(since = "1.7.0", feature = "path_strip_prefix")]
15731666
pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P)
15741667
-> Result<&'a Path, StripPrefixError>
@@ -1630,7 +1723,9 @@ impl Path {
16301723
iter_after(self.components().rev(), child.components().rev()).is_some()
16311724
}
16321725

1633-
/// Extracts the stem (non-extension) portion of `self.file_name()`.
1726+
/// Extracts the stem (non-extension) portion of [`self.file_name()`].
1727+
///
1728+
/// [`self.file_name()`]: struct.Path.html#method.file_name
16341729
///
16351730
/// The stem is:
16361731
///
@@ -1653,7 +1748,9 @@ impl Path {
16531748
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after))
16541749
}
16551750

1656-
/// Extracts the extension of `self.file_name()`, if possible.
1751+
/// Extracts the extension of [`self.file_name()`], if possible.
1752+
///
1753+
/// [`self.file_name()`]: struct.Path.html#method.file_name
16571754
///
16581755
/// The extension is:
16591756
///
@@ -1676,9 +1773,12 @@ impl Path {
16761773
self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.and(after))
16771774
}
16781775

1679-
/// Creates an owned `PathBuf` with `path` adjoined to `self`.
1776+
/// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
1777+
///
1778+
/// See [`PathBuf::push`] for more details on what it means to adjoin a path.
16801779
///
1681-
/// See `PathBuf::push` for more details on what it means to adjoin a path.
1780+
/// [`PathBuf`]: struct.PathBuf.html
1781+
/// [`PathBuf::push`]: struct.PathBuf.html#method.push
16821782
///
16831783
/// # Examples
16841784
///
@@ -1698,9 +1798,12 @@ impl Path {
16981798
buf
16991799
}
17001800

1701-
/// Creates an owned `PathBuf` like `self` but with the given file name.
1801+
/// Creates an owned [`PathBuf`] like `self` but with the given file name.
17021802
///
1703-
/// See `PathBuf::set_file_name` for more details.
1803+
/// See [`PathBuf::set_file_name`] for more details.
1804+
///
1805+
/// [`PathBuf`]: struct.PathBuf.html
1806+
/// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
17041807
///
17051808
/// # Examples
17061809
///
@@ -1721,9 +1824,12 @@ impl Path {
17211824
buf
17221825
}
17231826

1724-
/// Creates an owned `PathBuf` like `self` but with the given extension.
1827+
/// Creates an owned [`PathBuf`] like `self` but with the given extension.
1828+
///
1829+
/// See [`PathBuf::set_extension`] for more details.
17251830
///
1726-
/// See `PathBuf::set_extension` for more details.
1831+
/// [`PathBuf`]: struct.PathBuf.html
1832+
/// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
17271833
///
17281834
/// # Examples
17291835
///
@@ -1771,7 +1877,9 @@ impl Path {
17711877
}
17721878
}
17731879

1774-
/// Produce an iterator over the path's components viewed as `OsStr` slices.
1880+
/// Produce an iterator over the path's components viewed as [`OsStr`] slices.
1881+
///
1882+
/// [`OsStr`]: ffi/struct.OsStr.html
17751883
///
17761884
/// # Examples
17771885
///
@@ -1790,9 +1898,11 @@ impl Path {
17901898
Iter { inner: self.components() }
17911899
}
17921900

1793-
/// Returns an object that implements `Display` for safely printing paths
1901+
/// Returns an object that implements [`Display`] for safely printing paths
17941902
/// that may contain non-Unicode data.
17951903
///
1904+
/// [`Display`]: fmt/trait.Display.html
1905+
///
17961906
/// # Examples
17971907
///
17981908
/// ```
@@ -1854,11 +1964,13 @@ impl Path {
18541964

18551965
/// Returns an iterator over the entries within a directory.
18561966
///
1857-
/// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
1858-
/// be encountered after an iterator is initially constructed.
1967+
/// The iterator will yield instances of [`io::Result<`][`DirEntry`]`>`. New
1968+
/// errors may be encountered after an iterator is initially constructed.
18591969
///
18601970
/// This is an alias to [`fs::read_dir`].
18611971
///
1972+
/// [`io::Result<`]: ../io/type.Result.html
1973+
/// [`DirEntry`]: ../fs/struct.DirEntry.html
18621974
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
18631975
#[stable(feature = "path_ext", since = "1.5.0")]
18641976
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {

0 commit comments

Comments
 (0)