Skip to content

std::path::Path::starts_with documentation unclear #24882

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

Closed
0X1A opened this issue Apr 28, 2015 · 5 comments · Fixed by #25290
Closed

std::path::Path::starts_with documentation unclear #24882

0X1A opened this issue Apr 28, 2015 · 5 comments · Fixed by #25290

Comments

@0X1A
Copy link

0X1A commented Apr 28, 2015

The current documentation for std::path::Path::starts_with does not specify that it uses components as base. For example as described in the current documentation I expected to be able to use characters as base to find hidden files/directories as such:

fn is_dot(dir: &PathBuf) -> bool {
    if dir.starts_with(".") {
        return true;
    } else {
        return false
    }
}

fn main() {
    let path = PathBuf::from("/home/user/.dotfile");
    let rel_path = path.relative_from("/home/user").unwrap();
    println!("{}", path.display());
    println!("{}", rel_path.display());
    if is_dot(&PathBuf::from(rel_path)) {
        println!("{} is dot", rel_path.display());
    }
}

As described in the documentation I expected is_dot(&PathBuf::from(rel_path)) to be evaluated as true when it does not

@0X1A 0X1A changed the title std::path::Path::path::starts_with documentation unclear std::path::Path::starts_with documentation unclear Apr 28, 2015
@steveklabnik
Copy link
Member

Huh. This is very strange. @alexcrichton do you have any insight here?

@alexcrichton
Copy link
Member

Yes this is because everything's based around the iterators which need to be consumes exhaustively for matches like this to return true. I have also been bitten by this in the past though!

cc @aturon

@aturon
Copy link
Member

aturon commented May 1, 2015

The way I look at it is that the path API works with parsed, normalized paths, which consist of a number of components (between path separators). The entire API surface works this way. So starts_with, for example, implies that you could use join to get from the base path to the extended path -- and join likewise works at the component level, inserting a separator for each call.

I would rather add a starts_with method to OsStr, and then you can use as_os_str() to drop from a path view to a string view when you want that functionality.

@0X1A
Copy link
Author

0X1A commented May 4, 2015

@aturon I think a starts_with in OsStr that behaves like that of str's as you described would be very helpful. I still think the documentation for Path's starts_with should be more clear on how it operates using path components

@lilyball
Copy link
Contributor

lilyball commented May 4, 2015

I agree with @aturon, the current behavior of starts_with is correct, it just needs better documentation and perhaps a separate starts_with added to OsStr.

bluss pushed a commit to bluss/rust that referenced this issue May 10, 2015
bluss pushed a commit to bluss/rust that referenced this issue May 11, 2015
Manishearth added a commit to Manishearth/rust that referenced this issue May 11, 2015
Several Minor API / Reference Documentation Fixes

- Fix a few small errors in the reference.
- Fix paper cuts in the API docs.

Fixes rust-lang#24882
Fixes rust-lang#25233
Fixes rust-lang#25250
Manishearth added a commit to Manishearth/rust that referenced this issue May 11, 2015
Several Minor API / Reference Documentation Fixes

- Fix a few small errors in the reference.
- Fix paper cuts in the API docs.

Fixes rust-lang#24882
Fixes rust-lang#25233
Fixes rust-lang#25250
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 a pull request may close this issue.

5 participants