I'm still getting use to Rust's combinators for dealing with looping/option/result, and I'm finding interaction with break/continue to be ugly. I have a tool that supports passing regexes on the command line for filtering files. I found myself writing variations of this:
let basename_str = path
.file_name() // basename
.and_then(|x| x.to_str());
if basename_str.is_none() {
continue;
}
let basename_match = basename_regex.is_match(basename_str.unwrap());
let path_str = path.to_str();
if path_str.is_none() {
continue;
}
let path_match = path_regex.is_match(path_str.unwrap());
if basename_match || path_match {
// ...snip
}
I don't like the unwrap calls because I'm losing the guarantee of validity I would get by having an if let
or a match
, but nested if lets
push my code to the right of the screen, and match
won't bail early if the first to_str
fails (probably not hugely important in this example but you can imagine putting more expensive to check conditions after cheaper to check ones). I can't use ?
for shortcutting from anything because it will return, not continue.
I just overall get the feeling I'm doing this suboptimally. Is there a better way?