From acfe742c60d5846677a640808970d684021f380d Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 23 Dec 2024 13:58:31 +0900 Subject: [PATCH] feat: add with_replaced_extension as a alias of with_extension --- library/std/src/path.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 35e920ab34476..4af579b847db3 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2779,6 +2779,35 @@ impl Path { new_path } + /// Creates an owned [`PathBuf`] like `self` but with the extension replaced with given extension. + /// + /// See [`PathBuf::set_extension`] for more details. + /// + /// This function is exactly the same as [`Path::with_extension`], + /// but its name is ambiguous if the extension will be added or replaced. + /// + /// Once this function is stabilized, [`Path::with_extension`] will be deprecated. + /// + /// # Examples + /// + /// ``` + /// #![feature(path_add_extension)] + /// + /// use std::path::{Path, PathBuf}; + /// + /// let path = Path::new("foo.rs"); + /// assert_eq!(path.with_replaced_extension("txt"), PathBuf::from("foo.txt")); + /// + /// let path = Path::new("foo.tar.gz"); + /// assert_eq!(path.with_replaced_extension(""), PathBuf::from("foo.tar")); + /// assert_eq!(path.with_replaced_extension("xz"), PathBuf::from("foo.tar.xz")); + /// assert_eq!(path.with_replaced_extension("").with_replaced_extension("txt"), PathBuf::from("foo.txt")); + /// ``` + #[unstable(feature = "path_add_extension", issue = "127292")] + pub fn with_replaced_extension>(&self, extension: S) -> PathBuf { + self.with_extension(extension.as_ref()) + } + /// Creates an owned [`PathBuf`] like `self` but with the extension added. /// /// See [`PathBuf::add_extension`] for more details.