-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std docs: edit PathBuf::set_file_name
example
#109540
std docs: edit PathBuf::set_file_name
example
#109540
Conversation
r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Path::with_file_name
examplePath::with_file_name
example
1166f51
to
cb2e29f
Compare
cb2e29f
to
8f63abf
Compare
force-pushed because I called it "file name" instead of "file stem" in the commit msg. |
@@ -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"); |
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.
You don't have to change the extension at all.
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.
Hi John, the point of changing the extension here is to explicitly show that the subsequent call is overwriting the extension.
Before: foo.txt
-> bar.txt
.
After : foo.png
-> bar.txt
.
It's now clear that txt
was overwritten.
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.
Note that the extension is unrelated to the "file name". Methods for it don't care about the extension at all, and the current doc should tell it already by using foo.txt
and var
. And overwriting is explained by bar.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.
And overwriting is explained by bar.txt.
I agree, people should/could be able to understand overwriting by the bar.txt
part.
Although I can't find a real reason to revert this 3-letter change.
/// 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")); |
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.
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
than with_file_name
(but also changes with_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.
To make explicit that set_file_name might replace or remove the extension, not just the file stem.
The current doc doesn't mention stem and extension at all, and it also tells that it could overwrite everything by using bar
and baz.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.
The current doc doesn't mention stem and extension at all
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.
and it also tells that it could overwrite everything by using bar and baz.txt
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.
to show this method might replace or remove the extension, not just the file stem also edit docs of `Path::with_file_name` because it calls `PathBuf::set_file_name`
Path::with_file_name
examplePathBuf::set_file_name
example
8f63abf
to
4eb4e10
Compare
I think my changes would be helpful to other people reading the docs, so I'd like to keep them. Let's wait for @m-ou-se and see what she thinks. |
@bors r+ rollup |
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#109540 (std docs: edit `PathBuf::set_file_name` example) - rust-lang#110093 (Add 64-bit `time_t` support on 32-bit glibc Linux to `set_times`) - rust-lang#110987 (update wasi_clock_time_api ref.) - rust-lang#111038 (Leave promoteds untainted by errors when borrowck fails) - rust-lang#111042 (Add `#[no_coverage]` to the test harness's `fn main`) - rust-lang#111057 (Make sure the implementation of TcpStream::as_raw_fd is fully inlined) - rust-lang#111065 (Explicitly document how Send and Sync relate to references) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
To make explicit that
set_file_name
might replace or remove theextension, not just the file stem.
Also edit docs for
Path::with_file_name
, which callsset_file_name
.