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`.
This commit is contained in:
Matthias Krüger 2023-05-01 17:10:22 +02:00 committed by GitHub
commit 4da8a7a370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1395,11 +1395,16 @@ pub fn pop(&mut self) -> bool {
///
/// let mut buf = PathBuf::from("/");
/// assert!(buf.file_name() == None);
/// buf.set_file_name("bar");
/// assert!(buf == PathBuf::from("/bar"));
///
/// buf.set_file_name("foo.txt");
/// assert!(buf == PathBuf::from("/foo.txt"));
/// assert!(buf.file_name().is_some());
/// buf.set_file_name("baz.txt");
/// assert!(buf == PathBuf::from("/baz.txt"));
///
/// buf.set_file_name("bar.txt");
/// assert!(buf == PathBuf::from("/bar.txt"));
///
/// buf.set_file_name("baz");
/// assert!(buf == PathBuf::from("/baz"));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
@ -2562,7 +2567,8 @@ fn _join(&self, path: &Path) -> PathBuf {
/// ```
/// use std::path::{Path, PathBuf};
///
/// let path = Path::new("/tmp/foo.txt");
/// let path = Path::new("/tmp/foo.png");
/// assert_eq!(path.with_file_name("bar"), PathBuf::from("/tmp/bar"));
/// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
///
/// let path = Path::new("/tmp");