Skip to content

Documentation for std::path::PathBuf::pop is slightly wrong #58474

Closed

Description

@nathan

The documentation for std::path::PathBuf::pop() says:

Returns false and does nothing if self.file_name is None. Otherwise, returns true.

However, self.file_name() is None when the path ends in ..—but pop() still does removes that component and returns true. The docs should probably be changed to reflect that pop() truncates and returns true unless self.parent() is None.

use std::path::PathBuf;

fn main() {
  let mut p = PathBuf::from("/foo/bar/..");
  println!("{:?} {:?}", p.display(), p.file_name());
  println!("{:?}", p.pop());
  println!("{:?} {:?}", p.display(), p.file_name());
  println!("{:?}", p.pop());
  println!("{:?} {:?}", p.display(), p.file_name());
  println!("{:?}", p.pop());
  println!("{:?} {:?}", p.display(), p.file_name());
  println!("{:?}", p.pop());
  println!("{:?} {:?}", p.display(), p.file_name());
}

(Playground)

Output:

"/foo/bar/.." None
true
"/foo/bar" Some("bar")
true
"/foo" Some("foo")
true
"/" None
false
"/" None

Activity

  1. added
    A-docs
    on Feb 15, 2019
  2. hellow554 commented on Feb 15, 2019

    @hellow554
    Contributor

    @nathan would you open a PR and edit the required paragraph? Seems to be an easy issue :)

    https://github.com/rust-lang/rust/blob/master/src/libstd/path.rs#L1231-L1261

  3. nathan commented on Feb 15, 2019

    @nathan
    ContributorAuthor

    @hellow554 Sure thing! #58496

  4. added 2 commits that reference this issue on Feb 16, 2019
    fe03cf6
    98f1865
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions