Thomas Payne - Rust code snippets

Rust code snippets

fragments of Rust code which don't deserve their own library

Recursively search for files

Ignores symlinks

pub fn recurse_directories_for_files<P>(path: P) -> io::Result<Vec<std::fs::DirEntry>>
where
    P: AsRef<std::path::Path>,
{
    let mut ret = Vec::<_>::new();

    let entries = fs::read_dir(path)?
        .into_iter()
        .collect::<io::Result<Vec<_>>>()?;

    for entry in entries {
        let metadata = entry.metadata()?;

        if metadata.is_symlink() {
            continue;
        }

        if metadata.is_dir() {
            ret.append(&mut recurse_directories_for_files(entry.path())?);
        } else if metadata.is_file() {
            ret.push(entry);
        }
    }

    Ok(ret)
}