rust-lang / rust-lang/rust

Tracking Issue for implementing `DoubleEndedIterator` for `Ancestors`

Open
#153,070 1 comment 0 reactions 1 assignee View on GitHub

@asder8215 is already working on this.

Since Feb 25, 2026.

C-tracking-issue S-tracking-unimplemented T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Feature gate: #![feature(reverse_ancestor)]

This is a tracking issue for rust-lang/libs-team#745.

It implements the DoubleEndedIterator trait on the Ancestor in std::path, so that it's possible to traverse a path both in the back direction through path.ancestors()and in the forward direction through path.ancestors().rev(). Currently, the latter is not supported because Ancestors doesn't have a DoubleEndedIterator trait implemented.

Public API Example

With this ACP you can do something like:

use std::path::Path;
let path = Path::new("/foo/bar/text.txt");

for ancestor in path.ancestors() {
    println!("{:?}", ancestor);
}

for descendant in path.ancestors().rev() {
    println!("{:?}", descendant);
} 

Iterating through path.ancestors() would give:

"/foo/bar/text.txt"
"/foo/bar"
"/foo"
"/"

And iterating through path.ancestors().rev() would give:

"/"
"/foo"
"/foo/bar"
"/foo/bar/text.txt"

The benefit of this is that you don't need to do something like path.ancestors().collect()/path.ancestors().take(#).collect() to collect the ancestor paths into a Vec<Path> and be able to traverse the path in reverse direction (avoiding an allocation essentially).

Steps / History

(Remember to update the S-tracking-* label when checking boxes.)

Unresolved Questions
  • None yet.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.