Skip to content

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

Closed
@nathan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions