-
Notifications
You must be signed in to change notification settings - Fork 13.8k
std docs: edit PathBuf::set_file_name
example
#109540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1395,11 +1395,16 @@ impl PathBuf { | |
/// | ||
/// 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 @@ impl Path { | |
/// ``` | ||
/// 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"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be related to
with_file_name
and should be dropped.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I changed the PR title, description, and commit message to better indicate that this is about
PathBuf::set_file_name
thanwith_file_name
(but also changeswith_file_name
because that's a shorthand for the other method).I can split it on different PRs but I don't think that's necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current doc doesn't mention stem and extension at all, and it also tells that it could overwrite everything by using
bar
andbaz.txt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly! That's the point of the PR, make it explicit.
Would you prefer if this is explained in the method description instead of reflected in the examples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree, this is not stated as clearly as you're describing.
I'm trying to make it explicit because I was bitten by it recently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ultimately a question of "What is the filename?", and to explain it from scratch would be over-explaining it. We indeed have an explanation for stem but file name and extension are common things (I believe) and it seems less important here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this is very subjective.
What you might see as "over-explaining" some simple/common concepts, I'm seeing as "filling the gaps" in the documentation for something that is non-trivial.
It's reasonable for people to assume "file name" is actually the "file stem", and that
.set_file_name()
doesn't overwrite the extension, so I'm being explicit here.