Skip to content

Commit

Permalink
Rollup merge of rust-lang#41376 - mbrubeck:docs, r=frewsxcv
Browse files Browse the repository at this point in the history
Expanded docs and examples for PathBuf::file_name and friends

This addresses some common surprises when `PathBuf::set_file_name` is called on the path of a directory rather than a file.

r? @steveklabnik
  • Loading branch information
TimNN authored Apr 21, 2017
2 parents 38a8aa1 + 3b4d34d commit a3ac0c4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,13 @@ impl PathBuf {
/// If [`self.file_name`] was [`None`], this is equivalent to pushing
/// `file_name`.
///
/// Otherwise it is equivalent to calling [`pop`] and then pushing
/// `file_name`. The new path will be a sibling of the original path.
/// (That is, it will have the same parent.)
///
/// [`self.file_name`]: struct.PathBuf.html#method.file_name
/// [`None`]: ../../std/option/enum.Option.html#variant.None
/// [`pop`]: struct.PathBuf.html#method.pop
///
/// # Examples
///
Expand Down Expand Up @@ -1725,7 +1730,10 @@ impl Path {
})
}

/// Returns the final component of the `Path`, if it is a normal file.
/// Returns the final component of the `Path`, if there is one.
///
/// If the path is a normal file, this is the file name. If it's the path of a directory, this
/// is the directory name.
///
/// Returns [`None`] If the path terminates in `..`.
///
Expand All @@ -1737,10 +1745,12 @@ impl Path {
/// use std::path::Path;
/// use std::ffi::OsStr;
///
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt").file_name());
/// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
/// assert_eq!(None, Path::new("foo.txt/..").file_name());
/// assert_eq!(None, Path::new("/").file_name());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn file_name(&self) -> Option<&OsStr> {
Expand Down Expand Up @@ -1926,6 +1936,9 @@ impl Path {
///
/// let path = Path::new("/tmp/foo.txt");
/// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
///
/// let path = Path::new("/tmp");
/// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
Expand Down

0 comments on commit a3ac0c4

Please sign in to comment.