#pattern-match #returning #match #macro #options #convert #partial

match_opt

A macro for turning a partial match into a full match returning an option

3 releases

0.1.2 Feb 8, 2022
0.1.1 Feb 5, 2022
0.1.0 Feb 5, 2022

#1610 in Rust patterns

Download history 7395/week @ 2024-07-26 4768/week @ 2024-08-02 6605/week @ 2024-08-09 6767/week @ 2024-08-16 6724/week @ 2024-08-23 6792/week @ 2024-08-30 8276/week @ 2024-09-06 7852/week @ 2024-09-13 7891/week @ 2024-09-20 8047/week @ 2024-09-27 9624/week @ 2024-10-04 8532/week @ 2024-10-11 10868/week @ 2024-10-18 8630/week @ 2024-10-25 5636/week @ 2024-11-01 5335/week @ 2024-11-08

32,416 downloads per month

MIT license

5KB

match_opt Build Status

match_opt is a macro that converts a partial pattern-match into one that returns an Option. This can be used as a shorthand to simplify a complex pattern-match, for which only one or few cases are useful.

For example:

let some_results = vec!["1", "2", "3", "foo", "bar", "4"].map(|v| v.parse::<usize>());
let some_pattern_match = some_results.filter_map{|x| match x {
    Ok(v) if v > 2 => Some(v),
    _ => None,
}};
// this is equivalent to
let alternate_pattern_match = some_results.filter_map{|x| match_opt!(x, OK(v) if v > 2 => v)};

This crate has no dependencies.

Documentation

The API can be found here.

Installation

Add the following line to the dependencies of your Cargo.toml:

match_opt = "0.1.0"

Contributors

The authors of this code is François Garillot (@huitseeker).

License

This project is MIT licensed.

No runtime deps