Tracking Issue for implementing `DoubleEndedIterator` for `Ancestors`
@asder8215 is already working on this.
Since Feb 25, 2026.
- 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.)
- ACP: rust-lang/libs-team#745
- Implementation: https://github.com/rust-lang/rust/pull/153239
- Final comment period (FCP)^1
- Stabilization PR
Unresolved Questions
- None yet.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.