10
10
11
11
//! Cross-platform path manipulation.
12
12
//!
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
16
16
//! on strings according to the local platform's path syntax.
17
17
//!
18
18
//! ## Simple usage
19
19
//!
20
20
//! Path manipulation includes both parsing components from slices and building
21
21
//! new owned paths.
22
22
//!
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`]
24
24
//! slice and start asking questions:
25
25
//!
26
- //! ```rust
26
+ //! ```
27
27
//! use std::path::Path;
28
28
//! use std::ffi::OsStr;
29
29
//!
39
39
//! assert_eq!(extension, Some(OsStr::new("txt")));
40
40
//! ```
41
41
//!
42
- //! To build or modify paths, use `PathBuf`:
42
+ //! To build or modify paths, use [ `PathBuf`] :
43
43
//!
44
- //! ```rust
44
+ //! ```
45
45
//! use std::path::PathBuf;
46
46
//!
47
47
//! let mut path = PathBuf::from("c:\\");
103
103
//! that `b` is a symbolic link (so its parent isn't `a`). Further
104
104
//! normalization is possible to build on top of the components APIs,
105
105
//! 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
106
113
107
114
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
108
115
@@ -527,7 +534,9 @@ pub struct Components<'a> {
527
534
back : State ,
528
535
}
529
536
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
531
540
#[ derive( Clone ) ]
532
541
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
533
542
pub struct Iter < ' a > {
@@ -1089,10 +1098,11 @@ impl PathBuf {
1089
1098
1090
1099
/// Updates [`self.file_name()`] to `file_name`.
1091
1100
///
1092
- /// If [`self.file_name()`] was `None`, this is equivalent to pushing
1101
+ /// If [`self.file_name()`] was [ `None`] , this is equivalent to pushing
1093
1102
/// `file_name`.
1094
1103
///
1095
1104
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1105
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1096
1106
///
1097
1107
/// # Examples
1098
1108
///
@@ -1124,11 +1134,12 @@ impl PathBuf {
1124
1134
///
1125
1135
/// If [`self.file_name()`] is `None`, does nothing and returns `false`.
1126
1136
///
1127
- /// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
1137
+ /// Otherwise, returns `true`; if [`self.extension()`] is [ `None`] , the
1128
1138
/// extension is added; otherwise it is replaced.
1129
1139
///
1130
1140
/// [`self.file_name()`]: struct.PathBuf.html#method.file_name
1131
1141
/// [`self.extension()`]: struct.PathBuf.html#method.extension
1142
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1132
1143
///
1133
1144
/// # Examples
1134
1145
///
@@ -1356,8 +1367,10 @@ pub struct Path {
1356
1367
inner : OsStr ,
1357
1368
}
1358
1369
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
1360
1371
/// prefix was not found in `self`.
1372
+ ///
1373
+ /// [`Path::strip_prefix`]: struct.Path.html#method.strip_prefix
1361
1374
#[ derive( Debug , Clone , PartialEq , Eq ) ]
1362
1375
#[ stable( since = "1.7.0" , feature = "strip_prefix" ) ]
1363
1376
pub struct StripPrefixError ( ( ) ) ;
@@ -1539,7 +1552,9 @@ impl Path {
1539
1552
1540
1553
/// The path without its final component, if any.
1541
1554
///
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
1543
1558
///
1544
1559
/// # Examples
1545
1560
///
@@ -1570,7 +1585,9 @@ impl Path {
1570
1585
1571
1586
/// The final component of the path, if it is a normal file.
1572
1587
///
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
1574
1591
///
1575
1592
/// # Examples
1576
1593
///
@@ -1608,8 +1625,11 @@ impl Path {
1608
1625
///
1609
1626
/// # Errors
1610
1627
///
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
1613
1633
///
1614
1634
/// # Examples
1615
1635
///
@@ -1689,11 +1709,13 @@ impl Path {
1689
1709
///
1690
1710
/// The stem is:
1691
1711
///
1692
- /// * None, if there is no file name;
1712
+ /// * [` None`] , if there is no file name;
1693
1713
/// * The entire file name if there is no embedded `.`;
1694
1714
/// * The entire file name if the file name begins with `.` and has no other `.`s within;
1695
1715
/// * Otherwise, the portion of the file name before the final `.`
1696
1716
///
1717
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1718
+ ///
1697
1719
/// # Examples
1698
1720
///
1699
1721
/// ```
@@ -1710,15 +1732,16 @@ impl Path {
1710
1732
1711
1733
/// Extracts the extension of [`self.file_name()`], if possible.
1712
1734
///
1713
- /// [`self.file_name()`]: struct.Path.html#method.file_name
1714
- ///
1715
1735
/// The extension is:
1716
1736
///
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;
1720
1740
/// * Otherwise, the portion of the file name after the final `.`
1721
1741
///
1742
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1743
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1744
+ ///
1722
1745
/// # Examples
1723
1746
///
1724
1747
/// ```
@@ -1877,7 +1900,6 @@ impl Path {
1877
1900
Display { path : self }
1878
1901
}
1879
1902
1880
-
1881
1903
/// Query the file system to get information about a file, directory, etc.
1882
1904
///
1883
1905
/// This function will traverse symbolic links to query information about the
@@ -1886,6 +1908,16 @@ impl Path {
1886
1908
/// This is an alias to [`fs::metadata`].
1887
1909
///
1888
1910
/// [`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
+ /// ```
1889
1921
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1890
1922
pub fn metadata ( & self ) -> io:: Result < fs:: Metadata > {
1891
1923
fs:: metadata ( self )
@@ -1896,6 +1928,16 @@ impl Path {
1896
1928
/// This is an alias to [`fs::symlink_metadata`].
1897
1929
///
1898
1930
/// [`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
+ /// ```
1899
1941
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1900
1942
pub fn symlink_metadata ( & self ) -> io:: Result < fs:: Metadata > {
1901
1943
fs:: symlink_metadata ( self )
@@ -1907,6 +1949,15 @@ impl Path {
1907
1949
/// This is an alias to [`fs::canonicalize`].
1908
1950
///
1909
1951
/// [`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
+ /// ```
1910
1961
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1911
1962
pub fn canonicalize ( & self ) -> io:: Result < PathBuf > {
1912
1963
fs:: canonicalize ( self )
@@ -1917,6 +1968,15 @@ impl Path {
1917
1968
/// This is an alias to [`fs::read_link`].
1918
1969
///
1919
1970
/// [`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
+ /// ```
1920
1980
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1921
1981
pub fn read_link ( & self ) -> io:: Result < PathBuf > {
1922
1982
fs:: read_link ( self )
@@ -1932,6 +1992,19 @@ impl Path {
1932
1992
/// [`io::Result`]: ../io/type.Result.html
1933
1993
/// [`DirEntry`]: ../fs/struct.DirEntry.html
1934
1994
/// [`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
+ /// ```
1935
2008
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1936
2009
pub fn read_dir ( & self ) -> io:: Result < fs:: ReadDir > {
1937
2010
fs:: read_dir ( self )
0 commit comments