Struct FilterEntry
struct FilterEntry<I, P> { ... }
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>
fn filter_entry(self: 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_depthandmax_depthare not passed to this predicate.Note that if the iterator has
contents_firstenabled, then this method is no different than calling the standardIterator::filtermethod (because directory entries are yielded after they've been descended into).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_entryiterator adapter. (See its documentation for the same example functionality as above.)
impl<I> IntoIterator for FilterEntry<I, P>
fn into_iter(self: Self) -> I
impl<I, P> Freeze for FilterEntry<I, P>
impl<I, P> RefUnwindSafe for FilterEntry<I, P>
impl<I, P> Send for FilterEntry<I, P>
impl<I, P> Sync for FilterEntry<I, P>
impl<I, P> Unpin for FilterEntry<I, P>
impl<I, P> UnwindSafe for FilterEntry<I, P>
impl<I: $crate::fmt::Debug, P: $crate::fmt::Debug> Debug for FilterEntry<I, P>
fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result
impl<P> FusedIterator for FilterEntry<IntoIter, P>
impl<P> Iterator for FilterEntry<IntoIter, P>
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<T> Any for FilterEntry<I, P>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for FilterEntry<I, P>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for FilterEntry<I, P>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for FilterEntry<I, P>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for FilterEntry<I, P>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for FilterEntry<I, P>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for FilterEntry<I, P>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>