-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Insufficiently precise documentation of std::file!
macro
#113044
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
Comments
Whether it's an absolute or relative path depends on how rustc is invoked. Say you have a program like: pub fn main() {
println!("{}", file!());
} Then you store it in In cargo, I wasn't able to create an invocation that prints an absolute path. Correct me if I'm wrong, but I think cargo always changes the pwd and then passes relative paths to the program. One needs to also consider the include!("./b.rs");
fn main() {
foo();
} and a fn foo() {
println!("{}", file!());
} Then it will print So one cannot say that it's always absolute or always relative. That being said, docs could definitely be improved in that regard. They should however make the distinction between rustc and cargo clear, as not everyone uses rustc through cargo and the intended audience of the standard library docs is all standard library users, including those who use it through buck (folks at meta), bazel (fuchsia), or make (linux kernel). |
It will also depend on path-trimming. https://rust-lang.github.io/rfcs/3127-trim-paths.html |
|
It would indeed be quite useful to have a macro that always gives an absolute path, since otherwise one has to depend on how cargo picks the working directory for the rustc invocation, which has changed in the past and is AFAIK not a stable guarantee. |
Also see rust-lang/libs-team#478. |
Location
https://doc.rust-lang.org/std/macro.file.html
Summary
When describing properties of paths, it's useful to specify whether the given path is absolute or relative. After reading the documentation of
std::file
, which didn't mention whether it expands to absolute or relative path, I assumed (and rightly so!) that the path must be absolute. However, in my case it expanded tosrc/lib\nifti_image.rs
, to a relative path with alternating component separators. Based on that, I assume thatstd::file
expands to a path that is eitherSince it returns
&str
instead ofOsStr
, it would be weird if it would attempt to return absolute paths. However, this is not immediately obvious (to me).The text was updated successfully, but these errors were encountered: