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

It is not obvious how to construct a path from components #40159

Closed
nagisa opened this issue Feb 28, 2017 · 5 comments
Closed

It is not obvious how to construct a path from components #40159

nagisa opened this issue Feb 28, 2017 · 5 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools P-medium Medium priority

Comments

@nagisa
Copy link
Member

nagisa commented Feb 28, 2017

The only really cross-platform method to construct a path, is to construct one from components. For example os.path.join is a well known way to do so in Python.

In Rust is not as obvious how to do that in a convenient manner. You could push components one-by-one into a PathBuf, but that sounds sort of inconvenient. There’s a

impl<P: AsRef<Path>> FromIterator<P> for PathBuf

implementation exactly for that, but it is extremely difficult to discover. Perhaps what we need is a in-your-face example on PathBuf that explains the only really correct way to construct a multi-component PathBuf?

@durka
Copy link
Contributor

durka commented Feb 28, 2017

Is ["a", "b"].iter().collect::<PathBuf>() the best way?

@steveklabnik steveklabnik added A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools and removed A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Mar 10, 2017
@steveklabnik
Copy link
Member

docs team triage: second @durka 's question, @nagisa , that's what you're referring to?

@steveklabnik steveklabnik added the P-medium Medium priority label Mar 15, 2017
@nagisa
Copy link
Member Author

nagisa commented Mar 15, 2017

That’s the only way to construct a path with arbitrary number of components in a single statement without reallocating for every component that I could find. The other approach is to use Path::join, which will allocate a new PathBuf every time it is called, and therefore is to be avoided for this purpose. PathBuf::push needs a statement for each component.

@durka
Copy link
Contributor

durka commented Mar 15, 2017

Yeah let's put an example in the Path and PathBuf docs. p.join("x").join("y").join("z") sounds like a candidate for a Clippy lint.

@jdhorwitz
Copy link
Contributor

I will start working on this today.

steveklabnik added a commit to steveklabnik/rust that referenced this issue May 9, 2017
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
frewsxcv added a commit to frewsxcv/rust that referenced this issue 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 issue 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
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 P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

4 participants