use std::path::Path; use std::fs::{ReadDir, DirEntry}; use std::vec::*; use std::io; pub struct DirectoryIterator

{ root: P, dirs: Vec, } fn traverse

(path: P, out: &mut Vec) -> io::Result<()> where P:AsRef { for entry in std::fs::read_dir(path)? { match entry { Ok(entry) => out.push(entry), Err(_) => (), } } Ok(()) } impl

DirectoryIterator

where P: AsRef { pub fn begin(path: P) -> Result { let mut cache = Vec::new(); traverse(&path, &mut cache)?; Ok(Self{ root: path, dirs: cache, }) } } impl> Iterator for DirectoryIterator

{ type Item = String; fn next(&mut self) -> Option { let current = self.dirs.pop(); if let Some(current) = current { let path = current.path(); if path.is_dir() { let _ = traverse(&path, &mut self.dirs); self.next() } else { match path.as_path().to_str() { Some(path) => Some(path.to_string()), None => self.next(), } } } else { None } } }