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

Add more ways to create a PathBuf to docs #41531

Merged
merged 1 commit into from
May 10, 2017
Merged

Conversation

steveklabnik
Copy link
Member

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

@rust-highfive
Copy link
Collaborator

r? @BurntSushi

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -1036,14 +1044,40 @@ impl<'a> cmp::Ord for Components<'a> {
///
/// # Examples
///
/// You can use `push` to build up a `PathBuf` from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing link to push method.

/// path.set_extension("dll");
/// ```
///
/// However, `push` is best used for dynamic situations. This is a better way
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing link to push method.

/// let path = PathBuf::from("c:\\windows\system32.dll");
/// ```
///
/// Which method works best depends on what kind of sitaution you're in.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"sitaution"

@Mark-Simulacrum
Copy link
Member

It'd be nice to document how to create a PathBuf (or Path) from Components, since as far as I know it's currently not trivial, since there's no way to directly collect an iterator with Item=Component into Path/PathBuf.

@frewsxcv frewsxcv added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Apr 25, 2017
/// ```
/// use std::path::PathBuf;
///
/// let path = PathBuf::from("c:\\windows\system32.dll");
Copy link

@mzji mzji Apr 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@mzji mzji Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of { as escapes in format strings, not \\ as an escape, whoops, heh 😓

@nagisa
Copy link
Member

nagisa commented Apr 26, 2017 via email

@aidanhs aidanhs added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 26, 2017
@steveklabnik
Copy link
Member Author

I plan on updating this PR early next week; sorry about the delay.

@carols10cents
Copy link
Member

Looks like there's a test failure for you to fix when you get back to this o.O:

[01:01:42] failures:
[01:01:42] 
[01:01:42] ---- path.rs - path::PathBuf (line 1076) stdout ----
[01:01:42] 	error: unknown character escape: s
[01:01:42]  --> <anon>:6:39
[01:01:42]   |
[01:01:42] 6 | let path = PathBuf::from("c:\\windows\system32.dll");
[01:01:42]   |                                       ^
[01:01:42] 
[01:01:42] error: aborting due to previous error(s)
[01:01:42] 
[01:01:42] thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc/session/mod.rs:216
[01:01:42] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:01:42] 
[01:01:42] 
[01:01:42] failures:
[01:01:42]     path.rs - path::PathBuf (line 1076)
[01:01:42] 
[01:01:42] test result: FAILED. 783 passed; 1 failed; 22 ignored; 0 measured
[01:01:42] 
[01:01:42] error: test failed
[01:01:42] 

@steveklabnik
Copy link
Member Author

steveklabnik commented May 9, 2017

I believe I've fixed up everything; let's see what Travis says.

It'd be nice to document how to create a PathBuf (or Path) from Components, since as far as I know it's currently not trivial, since there's no way to directly collect an iterator with Item=Component into Path/PathBuf.

I agree this would be neat, but I don't know how either. I guess .map(as_os_str) and then collect from there? @rust-lang/libs any advice here?

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
@alexcrichton
Copy link
Member

I'd recommend Components::as_path

@Mark-Simulacrum
Copy link
Member

@alexcrichton The problem there is that it doesn't work as soon as you've filtered or done any other similar iterator operation on Components as far as I can tell; it'll only work if you have the "raw" Components iterator, which is non-ideal.

@alexcrichton
Copy link
Member

Ah in that case we have impl<P: AsRef<Path>> FromIterator<P> for PathBuf, so that could be used I believe?

/// ```
/// use std::path::PathBuf;
///
/// let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect();
Copy link
Member

@nagisa nagisa May 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The \ after C: should not be necessary here?

@Mark-Simulacrum
Copy link
Member

@alexcrichton No, that doesn't work either (https://is.gd/yG5UPk), since Component doesn't implement AsRef<Path> (it isn't a path, really, so this is understandable)--it feels like we need impl<P: AsRef<OsStr>> FromIterator<P> for PathBuf? Should I open a new issue about this?

@nagisa
Copy link
Member

nagisa commented May 9, 2017

it isn't a path, really, so this is understandable

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.

@nagisa
Copy link
Member

nagisa commented May 9, 2017

@bors r+ rollup

@Mark-Simulacrum could you please fill a separate issue for Component?

@bors
Copy link
Contributor

bors commented May 9, 2017

📌 Commit 23382e6 has been approved by nagisa

@Mark-Simulacrum
Copy link
Member

Filed #41866.

frewsxcv added a commit to frewsxcv/rust that referenced this pull request May 10, 2017
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
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 10, 2017
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
bors added a commit that referenced this pull request May 10, 2017
Rollup of 5 pull requests

- Successful merges: #41531, #41536, #41809, #41854, #41886
- Failed merges:
@bors bors merged commit 23382e6 into rust-lang:master May 10, 2017
@steveklabnik steveklabnik deleted the gh40159 branch October 25, 2017 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.