pub fn glob(pattern: &str) -> Result<Paths, PatternError>
Expand description
Return an iterator that produces all the Path
s that match the given
pattern using default match options, which may be absolute or relative to
the current working directory.
This may return an error if the pattern is invalid.
This method uses the default match options and is equivalent to calling
glob_with(pattern, MatchOptions::new())
. Use glob_with
directly if you
want to use non-default match options.
When iterating, each result is a GlobResult
which expresses the
possibility that there was an IoError
when attempting to read the contents
of the matched path. In other words, each item returned by the iterator
will either be an Ok(Path)
if the path matched, or an Err(GlobError)
if
the path (partially) matched but its contents could not be read in order
to determine if its contents matched.
See the Paths
documentation for more information.
Examples
Consider a directory /media/pictures
containing only the files
kittens.jpg
, puppies.jpg
and hamsters.gif
:
use glob::glob;
for entry in glob("/media/pictures/*.jpg").unwrap() {
match entry {
Ok(path) => println!("{:?}", path.display()),
// if the path matched but was unreadable,
// thereby preventing its contents from matching
Err(e) => println!("{:?}", e),
}
}
The above code will print:
/media/pictures/kittens.jpg
/media/pictures/puppies.jpg
If you want to ignore unreadable paths, you can use something like
filter_map
:
use glob::glob;
use std::result::Result;
for path in glob("/media/pictures/*.jpg").unwrap().filter_map(Result::ok) {
println!("{}", path.display());
}
Paths are yielded in alphabetical order.