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 ( ( ) ) ;
@@ -1534,7 +1547,9 @@ impl Path {
1534
1547
1535
1548
/// The path without its final component, if any.
1536
1549
///
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
1538
1553
///
1539
1554
/// # Examples
1540
1555
///
@@ -1565,7 +1580,9 @@ impl Path {
1565
1580
1566
1581
/// The final component of the path, if it is a normal file.
1567
1582
///
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
1569
1586
///
1570
1587
/// # Examples
1571
1588
///
@@ -1603,8 +1620,11 @@ impl Path {
1603
1620
///
1604
1621
/// # Errors
1605
1622
///
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
1608
1628
///
1609
1629
/// # Examples
1610
1630
///
@@ -1684,11 +1704,13 @@ impl Path {
1684
1704
///
1685
1705
/// The stem is:
1686
1706
///
1687
- /// * None, if there is no file name;
1707
+ /// * [` None`] , if there is no file name;
1688
1708
/// * The entire file name if there is no embedded `.`;
1689
1709
/// * The entire file name if the file name begins with `.` and has no other `.`s within;
1690
1710
/// * Otherwise, the portion of the file name before the final `.`
1691
1711
///
1712
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1713
+ ///
1692
1714
/// # Examples
1693
1715
///
1694
1716
/// ```
@@ -1705,15 +1727,16 @@ impl Path {
1705
1727
1706
1728
/// Extracts the extension of [`self.file_name()`], if possible.
1707
1729
///
1708
- /// [`self.file_name()`]: struct.Path.html#method.file_name
1709
- ///
1710
1730
/// The extension is:
1711
1731
///
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;
1715
1735
/// * Otherwise, the portion of the file name after the final `.`
1716
1736
///
1737
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
1738
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1739
+ ///
1717
1740
/// # Examples
1718
1741
///
1719
1742
/// ```
@@ -1872,7 +1895,6 @@ impl Path {
1872
1895
Display { path : self }
1873
1896
}
1874
1897
1875
-
1876
1898
/// Query the file system to get information about a file, directory, etc.
1877
1899
///
1878
1900
/// This function will traverse symbolic links to query information about the
@@ -1881,6 +1903,16 @@ impl Path {
1881
1903
/// This is an alias to [`fs::metadata`].
1882
1904
///
1883
1905
/// [`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
+ /// ```
1884
1916
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1885
1917
pub fn metadata ( & self ) -> io:: Result < fs:: Metadata > {
1886
1918
fs:: metadata ( self )
@@ -1891,6 +1923,16 @@ impl Path {
1891
1923
/// This is an alias to [`fs::symlink_metadata`].
1892
1924
///
1893
1925
/// [`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
+ /// ```
1894
1936
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1895
1937
pub fn symlink_metadata ( & self ) -> io:: Result < fs:: Metadata > {
1896
1938
fs:: symlink_metadata ( self )
@@ -1902,6 +1944,15 @@ impl Path {
1902
1944
/// This is an alias to [`fs::canonicalize`].
1903
1945
///
1904
1946
/// [`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
+ /// ```
1905
1956
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1906
1957
pub fn canonicalize ( & self ) -> io:: Result < PathBuf > {
1907
1958
fs:: canonicalize ( self )
@@ -1912,6 +1963,15 @@ impl Path {
1912
1963
/// This is an alias to [`fs::read_link`].
1913
1964
///
1914
1965
/// [`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
+ /// ```
1915
1975
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1916
1976
pub fn read_link ( & self ) -> io:: Result < PathBuf > {
1917
1977
fs:: read_link ( self )
@@ -1927,6 +1987,19 @@ impl Path {
1927
1987
/// [`io::Result`]: ../io/type.Result.html
1928
1988
/// [`DirEntry`]: ../fs/struct.DirEntry.html
1929
1989
/// [`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
+ /// ```
1930
2003
#[ stable( feature = "path_ext" , since = "1.5.0" ) ]
1931
2004
pub fn read_dir ( & self ) -> io:: Result < fs:: ReadDir > {
1932
2005
fs:: read_dir ( self )
0 commit comments