#variant #lib #extract #docs #macro #rs-extract-variant

macro extract-variant-internal

Internal lib for https://docs.rs/extract-variant

2 unstable releases

0.2.0 Dec 5, 2022
0.1.0 Dec 4, 2022

#264 in #extract

29 downloads per month
Used in extract-variant

MIT/Apache

4KB

Variant: Destructuring Macros

docs.rs Crates.io

This small crate exports two convenience macros. try_variant!, which can be used to destructure an expression into a single pattern, returning a result of assignments in the pattern. get_variant!, which works in the same way but returns an Option.

Both macros work similarly to the matches! macro from the rust std lib.

This macro is mainly useful for reducing matching boilerplate when you just want to try and extract some values from a pattern match (my reason for making this 😉).

Example

use variant::try_variant;

let val = Some((0, 1));
let res = try_variant!(val, Some((i, _))).expect("i");
assert_eq!(res, 0);

let res = try_variant!(val, Some((10, j)));
assert!(res.is_err());

There are more examples on the docs page


lib.rs:

Internal lib for variant.

Dependencies

~1.5MB
~33K SLoC