Skip to content

Commit 9a329e5

Browse files
committed
impl with_extra_extension for Path
Signed-off-by: tison <[email protected]>
1 parent da686fe commit 9a329e5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/std/src/path.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,30 @@ impl Path {
27022702
new_path
27032703
}
27042704

2705+
/// Creates an owned [`PathBuf`] like `self` but with an extra extension.
2706+
///
2707+
/// See [`PathBuf::add_extension`] for more details.
2708+
///
2709+
/// # Examples
2710+
///
2711+
/// ```
2712+
/// use std::path::{Path, PathBuf};
2713+
///
2714+
/// let path = Path::new("foo.rs");
2715+
/// assert_eq!(path.with_extra_extension("txt"), PathBuf::from("foo.rs.txt"));
2716+
///
2717+
/// let path = Path::new("foo.tar.gz");
2718+
/// assert_eq!(path.with_extra_extension(""), PathBuf::from("foo.tar.gz"));
2719+
/// assert_eq!(path.with_extra_extension("xz"), PathBuf::from("foo.tar.gz.xz"));
2720+
/// assert_eq!(path.with_extra_extension("").with_extra_extension("txt"), PathBuf::from("foo.tar.gz.txt"));
2721+
/// ```
2722+
#[stable(feature = "rust1", since = "1.0.0")]
2723+
pub fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
2724+
let mut new_path = self.to_path_buf();
2725+
new_path.add_extension(extension);
2726+
new_path
2727+
}
2728+
27052729
/// Produces an iterator over the [`Component`]s of the path.
27062730
///
27072731
/// When parsing the path, there is a small amount of normalization:

0 commit comments

Comments
 (0)