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

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

#1940 in Rust patterns

Download history 4636/week @ 2023-12-16 3450/week @ 2023-12-23 5127/week @ 2023-12-30 8817/week @ 2024-01-06 9734/week @ 2024-01-13 11065/week @ 2024-01-20 10860/week @ 2024-01-27 9285/week @ 2024-02-03 11173/week @ 2024-02-10 9353/week @ 2024-02-17 9175/week @ 2024-02-24 7289/week @ 2024-03-02 8865/week @ 2024-03-09 9747/week @ 2024-03-16 9869/week @ 2024-03-23 6285/week @ 2024-03-30

35,924 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