Skip to content

Commit 54535c5

Browse files
Rollup merge of rust-lang#38946 - GuillaumeGomez:path_doc, r=frewsxcv
Add missing links and examples for path modules and structs r? @frewsxcv
2 parents ef2c921 + 28d1ac3 commit 54535c5

File tree

1 file changed

+95
-22
lines changed

1 file changed

+95
-22
lines changed

src/libstd/path.rs

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
//! Cross-platform path manipulation.
1212
//!
13-
//! This module provides two types, `PathBuf` and `Path` (akin to `String` and
14-
//! `str`), for working with paths abstractly. These types are thin wrappers
15-
//! around `OsString` and `OsStr` respectively, meaning that they work directly
13+
//! This module provides two types, [`PathBuf`] and [`Path`][`Path`] (akin to [`String`]
14+
//! and [`str`]), for working with paths abstractly. These types are thin wrappers
15+
//! around [`OsString`] and [`OsStr`] respectively, meaning that they work directly
1616
//! on strings according to the local platform's path syntax.
1717
//!
1818
//! ## Simple usage
1919
//!
2020
//! Path manipulation includes both parsing components from slices and building
2121
//! new owned paths.
2222
//!
23-
//! To parse a path, you can create a `Path` slice from a `str`
23+
//! To parse a path, you can create a [`Path`] slice from a [`str`]
2424
//! slice and start asking questions:
2525
//!
26-
//! ```rust
26+
//! ```
2727
//! use std::path::Path;
2828
//! use std::ffi::OsStr;
2929
//!
@@ -39,9 +39,9 @@
3939
//! assert_eq!(extension, Some(OsStr::new("txt")));
4040
//! ```
4141
//!
42-
//! To build or modify paths, use `PathBuf`:
42+
//! To build or modify paths, use [`PathBuf`]:
4343
//!
44-
//! ```rust
44+
//! ```
4545
//! use std::path::PathBuf;
4646
//!
4747
//! let mut path = PathBuf::from("c:\\");
@@ -103,6 +103,13 @@
103103
//! that `b` is a symbolic link (so its parent isn't `a`). Further
104104
//! normalization is possible to build on top of the components APIs,
105105
//! and will be included in this library in the near future.
106+
//!
107+
//! [`PathBuf`]: ../../std/path/struct.PathBuf.html
108+
//! [`Path`]: ../../std/path/struct.Path.html
109+
//! [`String`]: ../../std/string/struct.String.html
110+
//! [`str`]: ../../std/primitive.str.html
111+
//! [`OsString`]: ../../std/ffi/struct.OsString.html
112+
//! [`OsStr`]: ../../std/ffi/struct.OsStr.html
106113
107114
#![stable(feature = "rust1", since = "1.0.0")]
108115

@@ -527,7 +534,9 @@ pub struct Components<'a> {
527534
back: State,
528535
}
529536

530-
/// An iterator over the components of a path, as `OsStr` slices.
537+
/// An iterator over the components of a path, as [`OsStr`] slices.
538+
///
539+
/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
531540
#[derive(Clone)]
532541
#[stable(feature = "rust1", since = "1.0.0")]
533542
pub struct Iter<'a> {
@@ -1089,10 +1098,11 @@ impl PathBuf {
10891098

10901099
/// Updates [`self.file_name()`] to `file_name`.
10911100
///
1092-
/// If [`self.file_name()`] was `None`, this is equivalent to pushing
1101+
/// If [`self.file_name()`] was [`None`], this is equivalent to pushing
10931102
/// `file_name`.
10941103
///
10951104
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1105+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
10961106
///
10971107
/// # Examples
10981108
///
@@ -1124,11 +1134,12 @@ impl PathBuf {
11241134
///
11251135
/// If [`self.file_name()`] is `None`, does nothing and returns `false`.
11261136
///
1127-
/// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
1137+
/// Otherwise, returns `true`; if [`self.extension()`] is [`None`], the
11281138
/// extension is added; otherwise it is replaced.
11291139
///
11301140
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
11311141
/// [`self.extension()`]: struct.PathBuf.html#method.extension
1142+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
11321143
///
11331144
/// # Examples
11341145
///
@@ -1356,8 +1367,10 @@ pub struct Path {
13561367
inner: OsStr,
13571368
}
13581369

1359-
/// An error returned from the `Path::strip_prefix` method indicating that the
1370+
/// An error returned from the [`Path::strip_prefix`] method indicating that the
13601371
/// prefix was not found in `self`.
1372+
///
1373+
/// [`Path::strip_prefix`]: struct.Path.html#method.strip_prefix
13611374
#[derive(Debug, Clone, PartialEq, Eq)]
13621375
#[stable(since = "1.7.0", feature = "strip_prefix")]
13631376
pub struct StripPrefixError(());
@@ -1539,7 +1552,9 @@ impl Path {
15391552

15401553
/// The path without its final component, if any.
15411554
///
1542-
/// Returns `None` if the path terminates in a root or prefix.
1555+
/// Returns [`None`] if the path terminates in a root or prefix.
1556+
///
1557+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
15431558
///
15441559
/// # Examples
15451560
///
@@ -1570,7 +1585,9 @@ impl Path {
15701585

15711586
/// The final component of the path, if it is a normal file.
15721587
///
1573-
/// If the path terminates in `..`, `file_name` will return `None`.
1588+
/// If the path terminates in `..`, `file_name` will return [`None`].
1589+
///
1590+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
15741591
///
15751592
/// # Examples
15761593
///
@@ -1608,8 +1625,11 @@ impl Path {
16081625
///
16091626
/// # Errors
16101627
///
1611-
/// If `base` is not a prefix of `self` (i.e. `starts_with`
1612-
/// returns `false`), returns `Err`.
1628+
/// If `base` is not a prefix of `self` (i.e. [`starts_with`]
1629+
/// returns `false`), returns [`Err`].
1630+
///
1631+
/// [`starts_with`]: #method.starts_with
1632+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
16131633
///
16141634
/// # Examples
16151635
///
@@ -1689,11 +1709,13 @@ impl Path {
16891709
///
16901710
/// The stem is:
16911711
///
1692-
/// * None, if there is no file name;
1712+
/// * [`None`], if there is no file name;
16931713
/// * The entire file name if there is no embedded `.`;
16941714
/// * The entire file name if the file name begins with `.` and has no other `.`s within;
16951715
/// * Otherwise, the portion of the file name before the final `.`
16961716
///
1717+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
1718+
///
16971719
/// # Examples
16981720
///
16991721
/// ```
@@ -1710,15 +1732,16 @@ impl Path {
17101732

17111733
/// Extracts the extension of [`self.file_name()`], if possible.
17121734
///
1713-
/// [`self.file_name()`]: struct.Path.html#method.file_name
1714-
///
17151735
/// The extension is:
17161736
///
1717-
/// * None, if there is no file name;
1718-
/// * None, if there is no embedded `.`;
1719-
/// * None, if the file name begins with `.` and has no other `.`s within;
1737+
/// * [`None`], if there is no file name;
1738+
/// * [`None`], if there is no embedded `.`;
1739+
/// * [`None`], if the file name begins with `.` and has no other `.`s within;
17201740
/// * Otherwise, the portion of the file name after the final `.`
17211741
///
1742+
/// [`self.file_name()`]: struct.Path.html#method.file_name
1743+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
1744+
///
17221745
/// # Examples
17231746
///
17241747
/// ```
@@ -1877,7 +1900,6 @@ impl Path {
18771900
Display { path: self }
18781901
}
18791902

1880-
18811903
/// Query the file system to get information about a file, directory, etc.
18821904
///
18831905
/// This function will traverse symbolic links to query information about the
@@ -1886,6 +1908,16 @@ impl Path {
18861908
/// This is an alias to [`fs::metadata`].
18871909
///
18881910
/// [`fs::metadata`]: ../fs/fn.metadata.html
1911+
///
1912+
/// # Examples
1913+
///
1914+
/// ```no_run
1915+
/// use std::path::Path;
1916+
///
1917+
/// let path = Path::new("/Minas/tirith");
1918+
/// let metadata = path.metadata().expect("metadata call failed");
1919+
/// println!("{:?}", metadata.file_type());
1920+
/// ```
18891921
#[stable(feature = "path_ext", since = "1.5.0")]
18901922
pub fn metadata(&self) -> io::Result<fs::Metadata> {
18911923
fs::metadata(self)
@@ -1896,6 +1928,16 @@ impl Path {
18961928
/// This is an alias to [`fs::symlink_metadata`].
18971929
///
18981930
/// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
1931+
///
1932+
/// # Examples
1933+
///
1934+
/// ```no_run
1935+
/// use std::path::Path;
1936+
///
1937+
/// let path = Path::new("/Minas/tirith");
1938+
/// let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
1939+
/// println!("{:?}", metadata.file_type());
1940+
/// ```
18991941
#[stable(feature = "path_ext", since = "1.5.0")]
19001942
pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
19011943
fs::symlink_metadata(self)
@@ -1907,6 +1949,15 @@ impl Path {
19071949
/// This is an alias to [`fs::canonicalize`].
19081950
///
19091951
/// [`fs::canonicalize`]: ../fs/fn.canonicalize.html
1952+
///
1953+
/// # Examples
1954+
///
1955+
/// ```no_run
1956+
/// use std::path::{Path, PathBuf};
1957+
///
1958+
/// let path = Path::new("/foo/test/../test/bar.rs");
1959+
/// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
1960+
/// ```
19101961
#[stable(feature = "path_ext", since = "1.5.0")]
19111962
pub fn canonicalize(&self) -> io::Result<PathBuf> {
19121963
fs::canonicalize(self)
@@ -1917,6 +1968,15 @@ impl Path {
19171968
/// This is an alias to [`fs::read_link`].
19181969
///
19191970
/// [`fs::read_link`]: ../fs/fn.read_link.html
1971+
///
1972+
/// # Examples
1973+
///
1974+
/// ```no_run
1975+
/// use std::path::Path;
1976+
///
1977+
/// let path = Path::new("/laputa/sky_castle.rs");
1978+
/// let path_link = path.read_link().expect("read_link call failed");
1979+
/// ```
19201980
#[stable(feature = "path_ext", since = "1.5.0")]
19211981
pub fn read_link(&self) -> io::Result<PathBuf> {
19221982
fs::read_link(self)
@@ -1932,6 +1992,19 @@ impl Path {
19321992
/// [`io::Result`]: ../io/type.Result.html
19331993
/// [`DirEntry`]: ../fs/struct.DirEntry.html
19341994
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
1995+
///
1996+
/// # Examples
1997+
///
1998+
/// ```no_run
1999+
/// use std::path::Path;
2000+
///
2001+
/// let path = Path::new("/laputa");
2002+
/// for entry in path.read_dir().expect("read_dir call failed") {
2003+
/// if let Ok(entry) = entry {
2004+
/// println!("{:?}", entry.path());
2005+
/// }
2006+
/// }
2007+
/// ```
19352008
#[stable(feature = "path_ext", since = "1.5.0")]
19362009
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
19372010
fs::read_dir(self)

0 commit comments

Comments
 (0)