Skip to content

Commit ffdd3b1

Browse files
authored
Rollup merge of #141477 - tshepang:patch-1, r=ChrisDenton
Path::with_extension: show that it adds an extension where one did no… …t exist I think the times I encountered this, I had to check first if files without extensions were added, since all examples only had files with existing extensions. Also, this replaced example already has a similar example below.
2 parents d9ed867 + 248f4b2 commit ffdd3b1

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

library/std/src/path.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,15 +2746,30 @@ impl Path {
27462746
/// # Examples
27472747
///
27482748
/// ```
2749-
/// use std::path::{Path, PathBuf};
2749+
/// use std::path::Path;
27502750
///
27512751
/// let path = Path::new("foo.rs");
2752-
/// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
2752+
/// assert_eq!(path.with_extension("txt"), Path::new("foo.txt"));
2753+
/// assert_eq!(path.with_extension(""), Path::new("foo"));
2754+
/// ```
2755+
///
2756+
/// Handling multiple extensions:
2757+
///
2758+
/// ```
2759+
/// use std::path::Path;
27532760
///
27542761
/// let path = Path::new("foo.tar.gz");
2755-
/// assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
2756-
/// assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
2757-
/// assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
2762+
/// assert_eq!(path.with_extension("xz"), Path::new("foo.tar.xz"));
2763+
/// assert_eq!(path.with_extension("").with_extension("txt"), Path::new("foo.txt"));
2764+
/// ```
2765+
///
2766+
/// Adding an extension where one did not exist:
2767+
///
2768+
/// ```
2769+
/// use std::path::Path;
2770+
///
2771+
/// let path = Path::new("foo");
2772+
/// assert_eq!(path.with_extension("rs"), Path::new("foo.rs"));
27582773
/// ```
27592774
#[stable(feature = "rust1", since = "1.0.0")]
27602775
pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {

0 commit comments

Comments
 (0)