Crates.io | extract-variant |
lib.rs | extract-variant |
version | 1.0.0 |
source | src |
created_at | 2022-12-04 00:54:00.417747 |
updated_at | 2023-04-07 18:09:42.274703 |
description | Destructure expressions into, and return assignments from, a single pattern. |
homepage | https://github.com/marcusdesai/variant |
repository | https://github.com/marcusdesai/variant |
max_upload_size | |
id | 729338 |
size | 23,869 |
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 😉).
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