Skip to content

feat: add with_replaced_extension as a alias of with_extension #134676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: AsRef<OsStr>>(&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.
Expand Down
Loading