Skip to content
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

Document that / works as separator on Windows #42955

Merged
merged 1 commit into from
Jun 29, 2017
Merged

Conversation

matklad
Copy link
Member

@matklad matklad commented Jun 28, 2017

Hi Whenever I see code like Path::new("./src/bin/main.rs") or path.ends_with("foo/bar"), I wonder if it will work on Windows as I expect. Unfortunately, reading the current docs does not help to answer this question, because all examples are Unix-specific.

However, I believe that using / is fine, because both Windows itself and Rust stdlib do treat it as a file separator, and because it is actually used in Cargo. So looks like we can just document it?

r? @steveklabnik

cc @retep998 I don't actually program for windows that much, so I might be totally wrong, and perhaps we should advise to always use (allocating) .join method to construct paths of more than one component?

@steveklabnik
Copy link
Member

Windows generally supports both, and I generally expect that Windows users know this, but there's no harm in pointing it out regardless! Thanks 😄

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Jun 28, 2017

📌 Commit 40dec09 has been approved by steveklabnik

@matklad
Copy link
Member Author

matklad commented Jun 28, 2017

Yeah, the problem is that I am not a windows user, but I do need to write cross platform software, so for me non-unix doc are more valuable exactly because I already know unix stuff :)

@steveklabnik
Copy link
Member

Absolutely! I meant it more of a "this is why there's a blind spot" than "we shouldn't put it in here".

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Jun 29, 2017
Document that `/` works as separator on Windows

Hi Whenever I see code like `Path::new("./src/bin/main.rs")` or `path.ends_with("foo/bar")`, I wonder if it will work on Windows as I expect. Unfortunately, reading the current docs does not help to answer this question, because all examples are Unix-specific.

However, I believe that using `/` is fine, because both Windows itself [and Rust stdlib](https://github.com/rust-lang/rust/blob/47faf1d51952ecd9d4c8a7325332fba34fbe00bd/src/libstd/sys/windows/path.rs#L26) do treat it as a file separator, and because it is [actually used](https://github.com/rust-lang/cargo/blob/abf01e1eddb3145c83f71b469ea7bee37141e5e1/tests/git.rs#L579) in Cargo. So looks like we can just document it?

r? @steveklabnik

cc @retep998 I don't actually program for windows that much, so I might be totally wrong, and perhaps we should advise to always use (allocating) `.join` method to construct paths of more than one component?
@retep998
Copy link
Member

While win32 paths in general support / as a path separator, verbatim paths are actually NT paths which only support \ as a path separator. Verbatim paths also don't support the special meaning of . or .., so they're a footgun in general.

arielb1 pushed a commit to arielb1/rust that referenced this pull request Jun 29, 2017
Document that `/` works as separator on Windows

Hi Whenever I see code like `Path::new("./src/bin/main.rs")` or `path.ends_with("foo/bar")`, I wonder if it will work on Windows as I expect. Unfortunately, reading the current docs does not help to answer this question, because all examples are Unix-specific.

However, I believe that using `/` is fine, because both Windows itself [and Rust stdlib](https://github.com/rust-lang/rust/blob/47faf1d51952ecd9d4c8a7325332fba34fbe00bd/src/libstd/sys/windows/path.rs#L26) do treat it as a file separator, and because it is [actually used](https://github.com/rust-lang/cargo/blob/abf01e1eddb3145c83f71b469ea7bee37141e5e1/tests/git.rs#L579) in Cargo. So looks like we can just document it?

r? @steveklabnik

cc @retep998 I don't actually program for windows that much, so I might be totally wrong, and perhaps we should advise to always use (allocating) `.join` method to construct paths of more than one component?
bors added a commit that referenced this pull request Jun 29, 2017
Rollup of 12 pull requests

- Successful merges: #42219, #42831, #42832, #42884, #42886, #42901, #42919, #42920, #42946, #42953, #42955, #42958
- Failed merges:
@bors bors merged commit 40dec09 into rust-lang:master Jun 29, 2017
@matklad
Copy link
Member Author

matklad commented Jul 10, 2017

Found at least one case where allocating Path::new("foo").join("bar").join("baz") is preferable over PathBuf::from("foo/bar/baz"). The latter will have / in .display() even on windows, which might be not what you want if you are showing this to the end user.

@matklad matklad deleted the doc-path branch July 9, 2019 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants