diff options
| author | steveklabnik <steve@steveklabnik.com> | 2017-04-25 06:38:26 -0400 |
|---|---|---|
| committer | steveklabnik <steve@steveklabnik.com> | 2017-05-09 12:43:49 -0400 |
| commit | 23382e614a6e6a2eaf3ac4c476ae48594de8538a (patch) | |
| tree | 4a7ffcfdd04f71657d19ca8177fa7f6fc19b6666 /src/libstd | |
| parent | f3fc547194d22dc673274ac20e9a7b1e607cb862 (diff) | |
Add more ways to create a PathBuf to docs
The best way to do this wasn't in the documentation, and the ways that were there needed some extra text to elaborate. Fixes #40159
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/path.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 9d66430bc93..f4b9a8972e3 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -51,10 +51,17 @@ //! ``` //! use std::path::PathBuf; //! +//! // This way works... //! let mut path = PathBuf::from("c:\\"); +//! //! path.push("windows"); //! path.push("system32"); +//! //! path.set_extension("dll"); +//! +//! // ... but push is best used if you don't know everything up +//! // front. If you do, this way is better: +//! let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect(); //! ``` //! //! [`Component`]: ../../std/path/enum.Component.html @@ -63,6 +70,7 @@ //! [`Path`]: ../../std/path/struct.Path.html //! [`push`]: ../../std/path/struct.PathBuf.html#method.push //! [`String`]: ../../std/string/struct.String.html +//! //! [`str`]: ../../std/primitive.str.html //! [`OsString`]: ../../std/ffi/struct.OsString.html //! [`OsStr`]: ../../std/ffi/struct.OsStr.html @@ -1036,14 +1044,40 @@ impl<'a> cmp::Ord for Components<'a> { /// /// # Examples /// +/// You can use [`push`] to build up a `PathBuf` from +/// components: +/// /// ``` /// use std::path::PathBuf; /// -/// let mut path = PathBuf::from("c:\\"); +/// let mut path = PathBuf::new(); +/// +/// path.push(r"C:\"); /// path.push("windows"); /// path.push("system32"); +/// /// path.set_extension("dll"); /// ``` +/// +/// However, [`push`] is best used for dynamic situations. This is a better way +/// to do this when you know all of the components ahead of time: +/// +/// ``` +/// use std::path::PathBuf; +/// +/// let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect(); +/// ``` +/// +/// We can still do better than this! Since these are all strings, we can use +/// `From::from`: +/// +/// ``` +/// use std::path::PathBuf; +/// +/// let path = PathBuf::from(r"C:\windows\system32.dll"); +/// ``` +/// +/// Which method works best depends on what kind of situation you're in. #[derive(Clone)] #[stable(feature = "rust1", since = "1.0.0")] pub struct PathBuf { |
