Skip to content

Commit e0da99f

Browse files
committed
impl with_extra_extension for Path
Signed-off-by: tison <[email protected]>
1 parent 35d2e1b commit e0da99f

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
@@ -2726,6 +2726,30 @@ impl Path {
27262726
new_path
27272727
}
27282728

2729+
/// Creates an owned [`PathBuf`] like `self` but with an extra extension.
2730+
///
2731+
/// See [`PathBuf::add_extension`] for more details.
2732+
///
2733+
/// # Examples
2734+
///
2735+
/// ```
2736+
/// use std::path::{Path, PathBuf};
2737+
///
2738+
/// let path = Path::new("foo.rs");
2739+
/// assert_eq!(path.with_extra_extension("txt"), PathBuf::from("foo.rs.txt"));
2740+
///
2741+
/// let path = Path::new("foo.tar.gz");
2742+
/// assert_eq!(path.with_extra_extension(""), PathBuf::from("foo.tar.gz"));
2743+
/// assert_eq!(path.with_extra_extension("xz"), PathBuf::from("foo.tar.gz.xz"));
2744+
/// assert_eq!(path.with_extra_extension("").with_extra_extension("txt"), PathBuf::from("foo.tar.gz.txt"));
2745+
/// ```
2746+
#[stable(feature = "rust1", since = "1.0.0")]
2747+
pub fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
2748+
let mut new_path = self.to_path_buf();
2749+
new_path.add_extension(extension);
2750+
new_path
2751+
}
2752+
27292753
/// Produces an iterator over the [`Component`]s of the path.
27302754
///
27312755
/// When parsing the path, there is a small amount of normalization:

0 commit comments

Comments
 (0)