about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-05-29 04:49:41 +0200
committerGitHub <noreply@github.com>2025-05-29 04:49:41 +0200
commitffdd3b16dca929e50f5de596d266da30f9a13d7a (patch)
treedd2643ab0b2882452d788e05aa46aa9997fed4dc
parentd9ed86729bdf70a588aec49cdd0576ed73bdb3ff (diff)
parent248f4b2ad2b18ab322748cc62b3524c109206b98 (diff)
downloadrust-ffdd3b16dca929e50f5de596d266da30f9a13d7a.tar.gz
rust-ffdd3b16dca929e50f5de596d266da30f9a13d7a.zip
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.
-rw-r--r--library/std/src/path.rs25
1 files changed, 20 insertions, 5 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index 2cdded1dfcf..050c617f564 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -2746,15 +2746,30 @@ impl Path {
     /// # Examples
     ///
     /// ```
-    /// use std::path::{Path, PathBuf};
+    /// use std::path::Path;
     ///
     /// let path = Path::new("foo.rs");
-    /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
+    /// assert_eq!(path.with_extension("txt"), Path::new("foo.txt"));
+    /// assert_eq!(path.with_extension(""), Path::new("foo"));
+    /// ```
+    ///
+    /// Handling multiple extensions:
+    ///
+    /// ```
+    /// use std::path::Path;
     ///
     /// let path = Path::new("foo.tar.gz");
-    /// assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
-    /// assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
-    /// assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
+    /// assert_eq!(path.with_extension("xz"), Path::new("foo.tar.xz"));
+    /// assert_eq!(path.with_extension("").with_extension("txt"), Path::new("foo.txt"));
+    /// ```
+    ///
+    /// Adding an extension where one did not exist:
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("foo");
+    /// assert_eq!(path.with_extension("rs"), Path::new("foo.rs"));
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {