Skip to content

Commit a3bd12b

Browse files
committed
Path::with_extension: improve examples
1 parent 3e674b0 commit a3bd12b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

library/std/src/path.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2743,12 +2743,27 @@ impl Path {
27432743
///
27442744
/// let path = Path::new("foo.rs");
27452745
/// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
2746+
/// assert_eq!(path.with_extension(""), PathBuf::from("foo"));
2747+
/// ```
2748+
///
2749+
/// Handling multiple extensions:
2750+
///
2751+
/// ```
2752+
/// use std::path::{Path, PathBuf};
27462753
///
27472754
/// let path = Path::new("foo.tar.gz");
2748-
/// assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
27492755
/// assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
27502756
/// assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
27512757
/// ```
2758+
///
2759+
/// Adding an extension where one did not exist:
2760+
///
2761+
/// ```
2762+
/// use std::path::{Path, PathBuf};
2763+
///
2764+
/// let path = Path::new("foo");
2765+
/// assert_eq!(path.with_extension("rs"), PathBuf::from("foo.rs"));
2766+
/// ```
27522767
#[stable(feature = "rust1", since = "1.0.0")]
27532768
pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
27542769
self._with_extension(extension.as_ref())

0 commit comments

Comments
 (0)