Function std::fs::read_dir [] [src]

pub fn read_dir<P: AsRef<Path>>(path: P) -> Result<ReadDir>

Returns an iterator over the entries within a directory.

The iterator will yield instances of io::Result<DirEntry>. New errors may be encountered after an iterator is initially constructed.

Examples

fn main() { use std::io; use std::fs::{self, DirEntry}; use std::path::Path; // one possible implementation of fs::walk_dir only visiting files fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> { if try!(fs::metadata(dir)).is_dir() { for entry in try!(fs::read_dir(dir)) { let entry = try!(entry); if try!(fs::metadata(entry.path())).is_dir() { try!(visit_dirs(&entry.path(), cb)); } else { cb(&entry); } } } Ok(()) } }
use std::io;
use std::fs::{self, DirEntry};
use std::path::Path;

// one possible implementation of fs::walk_dir only visiting files
fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
    if try!(fs::metadata(dir)).is_dir() {
        for entry in try!(fs::read_dir(dir)) {
            let entry = try!(entry);
            if try!(fs::metadata(entry.path())).is_dir() {
                try!(visit_dirs(&entry.path(), cb));
            } else {
                cb(&entry);
            }
        }
    }
    Ok(())
}

Errors

This function will return an error if the provided path doesn't exist, if the process lacks permissions to view the contents or if the path points at a non-directory file