diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2017-05-10 17:18:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-10 17:18:30 +0200 |
| commit | 292453223244142e3685908ddea8689517e5f85f (patch) | |
| tree | 63caa90c47c5048e459a41d1289f6ecfbd60fd7e | |
| parent | 25a161765fb90f2bfc78bda8fef944048e72bd26 (diff) | |
| parent | 23382e614a6e6a2eaf3ac4c476ae48594de8538a (diff) | |
| download | rust-292453223244142e3685908ddea8689517e5f85f.tar.gz rust-292453223244142e3685908ddea8689517e5f85f.zip | |
Rollup merge of #41531 - steveklabnik:gh40159, r=nagisa
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 /cc @nagisa
| -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 { |
