ReadDir reads only one record at a time, so it cannot sort it before iterating. There is no sorted ReadDir system call (at least on those platforms that I know of, which means portable can't be).
So, the only option is to read Vec and sort it:
use std::fs; fn main() { let mut paths: Vec<_> = fs::read_dir("/").unwrap() .map(|r| r.unwrap()) .collect(); paths.sort_by_key(|dir| dir.path()); for path in paths { println!("Name: {}", path.path().display()) } }
Chris emerson
source share