Struct IntoIter

struct IntoIter { ... }

An iterator for recursively descending into a directory.

A value with this type must be constructed with the WalkDir type, which uses a builder pattern to set options such as min/max depth, max open file descriptors and whether the iterator should follow symbolic links. After constructing a WalkDir, call .into_iter() at the end of the chain.

The order of elements yielded by this iterator is unspecified.

Implementations

impl IntoIter

fn skip_current_dir(self: &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.)

fn filter_entry<P>(self: Self, predicate: P) -> FilterEntry<Self, P>
where
    P: FnMut(&DirEntry) -> bool

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

impl Debug for IntoIter

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

impl Freeze for IntoIter

impl FusedIterator for IntoIter

impl Iterator for IntoIter

fn next(self: &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.

impl RefUnwindSafe for IntoIter

impl Send for IntoIter

impl Sync for IntoIter

impl Unpin for IntoIter

impl UnwindSafe for IntoIter

impl<I> IntoIterator for IntoIter

fn into_iter(self: Self) -> I

impl<T> Any for IntoIter

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for IntoIter

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for IntoIter

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

impl<T> From for IntoIter

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for IntoIter

fn into(self: 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 for IntoIter

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for IntoIter

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