Function which::which_re

source ·
pub fn which_re(
    regex: impl Borrow<Regex>
) -> Result<impl Iterator<Item = PathBuf>>
Expand description

Find all binaries matching a regular expression in a the system PATH.

Only available when feature regex is enabled.


  • regex - A regular expression to match binaries with


Find Python executables:

use regex::Regex;
use which::which;
use std::path::PathBuf;

let re = Regex::new(r"python\d$").unwrap();
let binaries: Vec<PathBuf> = which::which_re(re).unwrap().collect();
let python_paths = vec![PathBuf::from("/usr/bin/python2"), PathBuf::from("/usr/bin/python3")];
assert_eq!(binaries, python_paths);

Find all cargo subcommand executables on the path:

use which::which_re;
use regex::Regex;

    .for_each(|pth| println!("{}", pth.to_string_lossy()));