-
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
Improved documentation of Path::exists #80979
Changes from 3 commits
36672e1
a62309f
c6f83bf
5422764
fc4b83a
4682f32
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 |
---|---|---|
|
@@ -2386,13 +2386,27 @@ impl Path { | |
fs::read_dir(self) | ||
} | ||
|
||
/// Returns `true` if the path points at an existing entity. | ||
/// Returns `true` if the path points at an existing entity. False if it doesn't | ||
/// **or** we can't tell. | ||
/// | ||
/// This function will traverse symbolic links to query information about the | ||
/// destination file. In case of broken symbolic links this will return `false`. | ||
/// | ||
/// If you cannot access the directory containing the file, e.g., because of a | ||
/// permission error, this will return `false`. | ||
/// # Caveats | ||
/// | ||
/// Checking if a file exists and then attempting to use it is vulnerable to race | ||
/// conditions. It is better to attempt the operation and handle the error when it | ||
/// fails. | ||
/// | ||
/// If you cannot access the metadata of the file, e.g., because of a permission | ||
/// error on the containing directory, this will return `false`. In many cases this | ||
/// is not desirable as it silently ignores error that might be serious. | ||
/// | ||
/// Sometimes existence of a file is not what's actually interesting for an | ||
/// application but its accessibility. This function does **not** check if the file | ||
/// is accessible by the calling user. Calling `access` on Unix (or a similar | ||
/// syscall) would be more appropriate. Rust `std` currently does not provide such | ||
/// method. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -2405,6 +2419,20 @@ impl Path { | |
/// | ||
/// This is a convenience function that coerces errors to false. If you want to | ||
/// check errors, call [`fs::metadata`]. | ||
/// | ||
/// An example of proper error handling. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it perhaps make more sense to just link readers to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. My intention was to provide a simple snippet people could copy-paste to do the job properly. So I'd agree to moving the example to |
||
/// ```no_run | ||
/// use std::path::Path; | ||
/// use std::io::ErrorKind; | ||
/// use std::fs; | ||
/// let exists = match fs::metadata(Path::new("does_not_exist.txt")) { | ||
/// Ok(_) => true, | ||
/// Err(err) if err.kind() == ErrorKind::NotFound => false, | ||
/// Err(err) => return Err(err), | ||
/// }; | ||
/// | ||
/// assert!(!exists); | ||
/// ``` | ||
#[stable(feature = "path_ext", since = "1.5.0")] | ||
pub fn exists(&self) -> bool { | ||
fs::metadata(self).is_ok() | ||
|
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.
We don't generally call out the caveats as a separate section elsewhere in the documentation of the standard library, as it is very often implied (and similarly, the paragraph about traversing the symbolic links could also be considered a caveat, but is outside of this section).
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.
So just simple bold?
I actually don't find this behavior surprising at all as all other applications behave exactly same. Willing to reconsider if there's a bunch of people who find it surprising.
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 am on board with a Caveats heading here, it seems appropriate.