Skip to content

Commit 28d1ac3

Browse files
Add missing links and examples for path modules and structs
1 parent bd16aa0 commit 28d1ac3

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(());
@@ -1534,7 +1547,9 @@ impl Path {
15341547

15351548
/// The path without its final component, if any.
15361549
///
1537-
/// Returns `None` if the path terminates in a root or prefix.
1550+
/// Returns [`None`] if the path terminates in a root or prefix.
1551+
///
1552+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
15381553
///
15391554
/// # Examples
15401555
///
@@ -1565,7 +1580,9 @@ impl Path {
15651580

15661581
/// The final component of the path, if it is a normal file.
15671582
///
1568-
/// If the path terminates in `..`, `file_name` will return `None`.
1583+
/// If the path terminates in `..`, `file_name` will return [`None`].
1584+
///
1585+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
15691586
///
15701587
/// # Examples
15711588
///
@@ -1603,8 +1620,11 @@ impl Path {
16031620
///
16041621
/// # Errors
16051622
///
1606-
/// If `base` is not a prefix of `self` (i.e. `starts_with`
1607-
/// returns `false`), returns `Err`.
1623+
/// If `base` is not a prefix of `self` (i.e. [`starts_with`]
1624+
/// returns `false`), returns [`Err`].
1625+
///
1626+
/// [`starts_with`]: #method.starts_with
1627+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
16081628
///
16091629
/// # Examples
16101630
///
@@ -1684,11 +1704,13 @@ impl Path {
16841704
///
16851705
/// The stem is:
16861706
///
1687-
/// * None, if there is no file name;
1707+
/// * [`None`], if there is no file name;
16881708
/// * The entire file name if there is no embedded `.`;
16891709
/// * The entire file name if the file name begins with `.` and has no other `.`s within;
16901710
/// * Otherwise, the portion of the file name before the final `.`
16911711
///
1712+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
1713+
///
16921714
/// # Examples
16931715
///
16941716
/// ```
@@ -1705,15 +1727,16 @@ impl Path {
17051727

17061728
/// Extracts the extension of [`self.file_name()`], if possible.
17071729
///
1708-
/// [`self.file_name()`]: struct.Path.html#method.file_name
1709-
///
17101730
/// The extension is:
17111731
///
1712-
/// * None, if there is no file name;
1713-
/// * None, if there is no embedded `.`;
1714-
/// * None, if the file name begins with `.` and has no other `.`s within;
1732+
/// * [`None`], if there is no file name;
1733+
/// * [`None`], if there is no embedded `.`;
1734+
/// * [`None`], if the file name begins with `.` and has no other `.`s within;
17151735
/// * Otherwise, the portion of the file name after the final `.`
17161736
///
1737+
/// [`self.file_name()`]: struct.Path.html#method.file_name
1738+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
1739+
///
17171740
/// # Examples
17181741
///
17191742
/// ```
@@ -1872,7 +1895,6 @@ impl Path {
18721895
Display { path: self }
18731896
}
18741897

1875-
18761898
/// Query the file system to get information about a file, directory, etc.
18771899
///
18781900
/// This function will traverse symbolic links to query information about the
@@ -1881,6 +1903,16 @@ impl Path {
18811903
/// This is an alias to [`fs::metadata`].
18821904
///
18831905
/// [`fs::metadata`]: ../fs/fn.metadata.html
1906+
///
1907+
/// # Examples
1908+
///
1909+
/// ```no_run
1910+
/// use std::path::Path;
1911+
///
1912+
/// let path = Path::new("/Minas/tirith");
1913+
/// let metadata = path.metadata().expect("metadata call failed");
1914+
/// println!("{:?}", metadata.file_type());
1915+
/// ```
18841916
#[stable(feature = "path_ext", since = "1.5.0")]
18851917
pub fn metadata(&self) -> io::Result<fs::Metadata> {
18861918
fs::metadata(self)
@@ -1891,6 +1923,16 @@ impl Path {
18911923
/// This is an alias to [`fs::symlink_metadata`].
18921924
///
18931925
/// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
1926+
///
1927+
/// # Examples
1928+
///
1929+
/// ```no_run
1930+
/// use std::path::Path;
1931+
///
1932+
/// let path = Path::new("/Minas/tirith");
1933+
/// let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
1934+
/// println!("{:?}", metadata.file_type());
1935+
/// ```
18941936
#[stable(feature = "path_ext", since = "1.5.0")]
18951937
pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
18961938
fs::symlink_metadata(self)
@@ -1902,6 +1944,15 @@ impl Path {
19021944
/// This is an alias to [`fs::canonicalize`].
19031945
///
19041946
/// [`fs::canonicalize`]: ../fs/fn.canonicalize.html
1947+
///
1948+
/// # Examples
1949+
///
1950+
/// ```no_run
1951+
/// use std::path::{Path, PathBuf};
1952+
///
1953+
/// let path = Path::new("/foo/test/../test/bar.rs");
1954+
/// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
1955+
/// ```
19051956
#[stable(feature = "path_ext", since = "1.5.0")]
19061957
pub fn canonicalize(&self) -> io::Result<PathBuf> {
19071958
fs::canonicalize(self)
@@ -1912,6 +1963,15 @@ impl Path {
19121963
/// This is an alias to [`fs::read_link`].
19131964
///
19141965
/// [`fs::read_link`]: ../fs/fn.read_link.html
1966+
///
1967+
/// # Examples
1968+
///
1969+
/// ```no_run
1970+
/// use std::path::Path;
1971+
///
1972+
/// let path = Path::new("/laputa/sky_castle.rs");
1973+
/// let path_link = path.read_link().expect("read_link call failed");
1974+
/// ```
19151975
#[stable(feature = "path_ext", since = "1.5.0")]
19161976
pub fn read_link(&self) -> io::Result<PathBuf> {
19171977
fs::read_link(self)
@@ -1927,6 +1987,19 @@ impl Path {
19271987
/// [`io::Result`]: ../io/type.Result.html
19281988
/// [`DirEntry`]: ../fs/struct.DirEntry.html
19291989
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
1990+
///
1991+
/// # Examples
1992+
///
1993+
/// ```no_run
1994+
/// use std::path::Path;
1995+
///
1996+
/// let path = Path::new("/laputa");
1997+
/// for entry in path.read_dir().expect("read_dir call failed") {
1998+
/// if let Ok(entry) = entry {
1999+
/// println!("{:?}", entry.path());
2000+
/// }
2001+
/// }
2002+
/// ```
19302003
#[stable(feature = "path_ext", since = "1.5.0")]
19312004
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
19322005
fs::read_dir(self)

0 commit comments

Comments
 (0)