about summary refs log tree commit diff
path: root/library/std/src/path.rs
diff options
context:
space:
mode:
authorTshepang Mbambo <hopsi@tuta.io>2025-05-24 22:12:15 +0200
committerTshepang Mbambo <hopsi@tuta.io>2025-05-24 22:12:15 +0200
commita3bd12b88aaf32b1036cdcb2ef3efe509dbebc15 (patch)
treed2664578a6db9682f83d996e4f6b2ac0e593d8e5 /library/std/src/path.rs
parent3e674b06b5c74adea662bd0b0b06450757994b16 (diff)
downloadrust-a3bd12b88aaf32b1036cdcb2ef3efe509dbebc15.tar.gz
rust-a3bd12b88aaf32b1036cdcb2ef3efe509dbebc15.zip
Path::with_extension: improve examples
Diffstat (limited to 'library/std/src/path.rs')
-rw-r--r--library/std/src/path.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index 7959c633858..0583ee1eb32 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -2743,12 +2743,27 @@ impl Path {
     ///
     /// let path = Path::new("foo.rs");
     /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
+    /// assert_eq!(path.with_extension(""), PathBuf::from("foo"));
+    /// ```
+    ///
+    /// Handling multiple extensions:
+    ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
     ///
     /// 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"));
     /// ```
+    ///
+    /// Adding an extension where one did not exist:
+    ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let path = Path::new("foo");
+    /// assert_eq!(path.with_extension("rs"), PathBuf::from("foo.rs"));
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
         self._with_extension(extension.as_ref())