Trait walkdir::WalkDirIterator [] [src]

pub trait WalkDirIterator: Iterator {
    fn skip_current_dir(&mut self);

    fn filter_entry<P>(self, predicate: P) -> IterFilterEntry<Self, P> where Self: Sized, P: FnMut(&DirEntry) -> bool { ... }
}

A trait for recursive directory iterators.

Required Methods

Skips the current directory.

This causes the iterator to stop traversing the contents of the least recently yielded directory. This means any remaining entries in that directory will be skipped (including sub-directories).

Note that the ergnomics of this method are questionable since it borrows the iterator mutably. Namely, you must write out the looping condition manually. For example, to skip hidden entries efficiently on unix systems:

use walkdir::{DirEntry, WalkDir, WalkDirIterator};

fn is_hidden(entry: &DirEntry) -> bool {
    entry.file_name()
         .to_str()
         .map(|s| s.starts_with("."))
         .unwrap_or(false)
}

let mut it = WalkDir::new("foo").into_iter();
loop {
    let entry = match it.next() {
        None => break,
        Some(Err(err)) => panic!("ERROR: {}", err),
        Some(Ok(entry)) => entry,
    };
    if is_hidden(&entry) {
        if entry.file_type().is_dir() {
            it.skip_current_dir();
        }
        continue;
    }
    println!("{}", entry.path().display());
}

You may find it more convenient to use the filter_entry iterator adapter. (See its documentation for the same example functionality as above.)

Provided Methods

Yields only entries which satisfy the given predicate and skips descending into directories that do not satisfy the given predicate.

The predicate is applied to all entries. If the predicate is true, iteration carries on as normal. If the predicate is false, the entry is ignored and if it is a directory, it is not descended into.

This is often more convenient to use than skip_current_dir. For example, to skip hidden files and directories efficiently on unix systems:

use walkdir::{DirEntry, WalkDir, WalkDirIterator};

fn is_hidden(entry: &DirEntry) -> bool {
    entry.file_name()
         .to_str()
         .map(|s| s.starts_with("."))
         .unwrap_or(false)
}

for entry in WalkDir::new("foo")
                     .into_iter()
                     .filter_entry(|e| !is_hidden(e)) {
    let entry = entry.unwrap();
    println!("{}", entry.path().display());
}

Note that the iterator will still yield errors for reading entries that may not satisfy the predicate.

Note that entries skipped with min_depth and max_depth are not passed to this predicate.

Implementors