Module glob

Source
Expand description

Unix shell style pattern matching.


glob provides support for matching file paths against Unix shell style patterns. The crate offers filesystem iteration with pattern matching, similar to the glob function in libc, as well as direct pattern matching against individual paths, similar to fnmatch.

The main entry points are the glob and glob_with functions, which return iterators over filesystem paths matching a pattern. For direct pattern matching without filesystem access, the Pattern type can be compiled and used to test individual paths.

Glob patterns support wildcards like * for any sequence of characters, ? for any single character, and ** for recursive directory matching. Character sets with [] and brace expansion with {} are also supported.

§Examples

Finding all Rust source files recursively:

use glob::glob;

for entry in glob("**/*.rs").expect("Failed to read glob pattern") {
    match entry {
        Ok(path) => println!("{:?}", path.display()),
        Err(e) => println!("{:?}", e),
    }
}

Matching paths against a pattern:

use glob::Pattern;

let pattern = Pattern::new("*.txt").unwrap();
assert!(pattern.matches("hello.txt"));
assert!(pattern.matches("document.txt"));
assert!(!pattern.matches("image.png"));

Using custom match options:

use glob::{glob_with, MatchOptions};

let options = MatchOptions {
    case_sensitive: false,
    ..Default::default()
};

for entry in glob_with("*.TXT", options).expect("Failed to read glob pattern") {
    println!("{:?}", entry);
}

Structs§

GlobError
A glob iteration error.
MatchOptions
Configuration options to modify the behaviour of Pattern::matches_with(..).
Paths
An iterator that yields Paths from the filesystem that match a particular pattern.
Pattern
A compiled Unix shell style pattern.
PatternError
A pattern parsing error.

Functions§

glob
Return an iterator that produces all the Paths that match the given pattern using default match options, which may be absolute or relative to the current working directory.
glob_with
Return an iterator that produces all the Paths that match the given pattern using the specified match options, which may be absolute or relative to the current working directory.

Type Aliases§

GlobResult
An alias for a glob iteration result.