Skip to content

Commit 4da8a7a

Browse files
authored
Rollup merge of #109540 - marcospb19:edit-Path-with_file_name-example, r=m-ou-se
std docs: edit `PathBuf::set_file_name` example To make explicit that `set_file_name` might replace or remove the extension, not just the file stem. Also edit docs for `Path::with_file_name`, which calls `set_file_name`.
2 parents b7d8c88 + 4eb4e10 commit 4da8a7a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

library/std/src/path.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1395,11 +1395,16 @@ impl PathBuf {
13951395
///
13961396
/// let mut buf = PathBuf::from("/");
13971397
/// assert!(buf.file_name() == None);
1398-
/// buf.set_file_name("bar");
1399-
/// assert!(buf == PathBuf::from("/bar"));
1398+
///
1399+
/// buf.set_file_name("foo.txt");
1400+
/// assert!(buf == PathBuf::from("/foo.txt"));
14001401
/// assert!(buf.file_name().is_some());
1401-
/// buf.set_file_name("baz.txt");
1402-
/// assert!(buf == PathBuf::from("/baz.txt"));
1402+
///
1403+
/// buf.set_file_name("bar.txt");
1404+
/// assert!(buf == PathBuf::from("/bar.txt"));
1405+
///
1406+
/// buf.set_file_name("baz");
1407+
/// assert!(buf == PathBuf::from("/baz"));
14031408
/// ```
14041409
#[stable(feature = "rust1", since = "1.0.0")]
14051410
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
@@ -2562,7 +2567,8 @@ impl Path {
25622567
/// ```
25632568
/// use std::path::{Path, PathBuf};
25642569
///
2565-
/// let path = Path::new("/tmp/foo.txt");
2570+
/// let path = Path::new("/tmp/foo.png");
2571+
/// assert_eq!(path.with_file_name("bar"), PathBuf::from("/tmp/bar"));
25662572
/// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
25672573
///
25682574
/// let path = Path::new("/tmp");

0 commit comments

Comments
 (0)