Struct FilterEntry

pub struct FilterEntry<I, P> { /* private fields */ }

A recursive directory iterator that skips entries.

Values of this type are created by calling .filter_entry() on an IntoIter, which is formed by calling .into_iter() on a WalkDir.

Directories that fail the predicate P are skipped. Namely, they are never yielded and never descended into.

Entries that are skipped with the min_depth and max_depth options are not passed through this filter.

If opening a handle to a directory resulted in an error, then it is yielded and no corresponding call to the predicate is made.

Type parameter I refers to the underlying iterator and P refers to the predicate, which is usually FnMut(&DirEntry) -> bool.

Implementations

impl<P> FilterEntry<IntoIter, P> where P: FnMut(&DirEntry) -> bool,

fn filter_entry(self, predicate: P) -> FilterEntry<Self, P>

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};
# use walkdir::Error;

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

# fn try_main() -> Result<(), Error> {
for entry in WalkDir::new("foo")
                     .into_iter()
                     .filter_entry(|e| !is_hidden(e)) {
    println!("{}", entry?.path().display());
}
# Ok(())
# }

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.

Note that if the iterator has contents_first enabled, then this method is no different than calling the standard Iterator::filter method (because directory entries are yielded after they've been descended into).

fn skip_current_dir(&mut self)

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 ergonomics 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};

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.)

Trait Implementations

impl<I: Debug, P: Debug> Debug for FilterEntry<I, P>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<P> FusedIterator for FilterEntry<IntoIter, P> where P: FnMut(&DirEntry) -> bool,

impl<P> Iterator for FilterEntry<IntoIter, P> where P: FnMut(&DirEntry) -> bool,

type Item = Result<DirEntry, Error>;
fn next(&mut self) -> Option<Result<DirEntry>>

Advances the iterator and returns the next value.

Errors

If the iterator fails to retrieve the next value, this method returns an error value. The error will be wrapped in an Option::Some.

Auto Trait Implementations

impl<I, P> Freeze for FilterEntry<I, P> where I: Freeze, P: Freeze,

impl<I, P> RefUnwindSafe for FilterEntry<I, P> where I: RefUnwindSafe, P: RefUnwindSafe,

impl<I, P> Send for FilterEntry<I, P> where I: Send, P: Send,

impl<I, P> Sync for FilterEntry<I, P> where I: Sync, P: Sync,

impl<I, P> Unpin for FilterEntry<I, P> where I: Unpin, P: Unpin,

impl<I, P> UnsafeUnpin for FilterEntry<I, P> where I: UnsafeUnpin, P: UnsafeUnpin,

impl<I, P> UnwindSafe for FilterEntry<I, P> where I: UnwindSafe, P: UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for FilterEntry<I, P> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for FilterEntry<I, P> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for FilterEntry<I, P> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for FilterEntry<I, P> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for FilterEntry<I, P>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for FilterEntry<I, P> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for FilterEntry<I, P> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for FilterEntry<I, P> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>