Add more ways to create a PathBuf to docs#41531
Conversation
|
r? @BurntSushi (rust_highfive has picked a reviewer for you, use r? to override) |
src/libstd/path.rs
Outdated
There was a problem hiding this comment.
Missing link to push method.
src/libstd/path.rs
Outdated
There was a problem hiding this comment.
Missing link to push method.
src/libstd/path.rs
Outdated
|
It'd be nice to document how to create a |
src/libstd/path.rs
Outdated
There was a problem hiding this comment.
Shouldn't the path be c:\\windows\\system32.dll ?
BTW, on windows we prefer to write the drive letters as uppercase. How about change the path to C:\\Windows\\system32\\user32.dll ? It's a real file path which exists on almost all Windows installations today.
There was a problem hiding this comment.
on windows we prefer to write the drive letters as uppercase.
I thought so too (I'm a Windows user now) but since it was small before, I didn't change it. Happy to do so
Also, if I check in explorer.exe, C:\\windows\ does not load, but C:\windows\ does, so maybe it should be the other way? C:\\windows\\ doesn't load either.
There was a problem hiding this comment.
Since Rust is a language which works on multiple platforms, I think that following the conventions on different platform is useful and important.
Here the \\ is a escape sequence in the path string, so the real path is C:\Windows\ (note the path fragment Windows is capitalized since on Windows the name of this directory is capitailized by default).
There was a problem hiding this comment.
I was thinking of { as escapes in format strings, not \\ as an escape, whoops, heh 😓
|
I would suggest using raw string to avoid escapes.
…On Apr 26, 2017 12:49 PM, "mzji" ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/libstd/path.rs
<#41531 (comment)>:
> +/// However, `push` is best used for dynamic situations. This is a better way
+/// to do this when you know all of the components ahead of time:
+///
+/// ```
+/// use std::path::PathBuf;
+///
+/// let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect();
+/// ```
+///
+/// We can still do better than this! Since these are all strings, we can use
+/// `From::from`:
+///
+/// ```
+/// use std::path::PathBuf;
+///
+/// let path = PathBuf::from("c:\\windows\system32.dll");
Here the \\ is a escape sequence in the path string, so the real path *is*
C:\Windows\ (note the path fragment Windows is capitalized since on
Windows the name of this directory is capitailized by default).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#41531 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AApc0rOkpDhRbXAT7voA5o4N1LyTJDM4ks5rzxNEgaJpZM4NHTJ_>
.
|
|
I plan on updating this PR early next week; sorry about the delay. |
|
Looks like there's a test failure for you to fix when you get back to this o.O: |
|
I believe I've fixed up everything; let's see what Travis says.
I agree this would be neat, but I don't know how either. I guess |
The best way to do this wasn't in the documentation, and the ways that were there needed some extra text to elaborate. Fixes rust-lang#40159
|
I'd recommend |
|
@alexcrichton The problem there is that it doesn't work as soon as you've filtered or done any other similar iterator operation on |
|
Ah in that case we have |
| /// ``` | ||
| /// use std::path::PathBuf; | ||
| /// | ||
| /// let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect(); |
There was a problem hiding this comment.
The \ after C: should not be necessary here?
|
@alexcrichton No, that doesn't work either (https://is.gd/yG5UPk), since |
Component of a path is always a valid path. The changes to me seem good and I’d r+; it seems to me like discussion about Components could be separate. |
|
@bors r+ rollup @Mark-Simulacrum could you please fill a separate issue for |
|
📌 Commit 23382e6 has been approved by |
|
Filed #41866. |
Add more ways to create a PathBuf to docs The best way to do this wasn't in the documentation, and the ways that were there needed some extra text to elaborate. Fixes rust-lang#40159 /cc @nagisa
Add more ways to create a PathBuf to docs The best way to do this wasn't in the documentation, and the ways that were there needed some extra text to elaborate. Fixes rust-lang#40159 /cc @nagisa
The best way to do this wasn't in the documentation, and the ways that
were there needed some extra text to elaborate.
Fixes #40159
/cc @nagisa