Skip to content

Commit 409a10a

Browse files
committed
Implemented Path::with_file_name
1 parent 3c24b18 commit 409a10a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/path/path.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,28 @@ impl Path {
728728
pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
729729
self.inner.with_extension(extension).into()
730730
}
731+
732+
/// Creates an owned [`PathBuf`] like `self` but with the given file name.
733+
///
734+
/// See [`PathBuf::set_file_name`] for more details.
735+
///
736+
/// [`PathBuf`]: struct.PathBuf.html
737+
/// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
738+
///
739+
/// # Examples
740+
///
741+
/// ```
742+
/// use async_std::path::{Path, PathBuf};
743+
///
744+
/// let path = Path::new("/tmp/foo.txt");
745+
/// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
746+
///
747+
/// let path = Path::new("/tmp");
748+
/// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
749+
/// ```
750+
pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
751+
self.inner.with_file_name(file_name).into()
752+
}
731753
}
732754

733755
impl<'a> From<&'a std::path::Path> for &'a Path {

0 commit comments

Comments
 (0)