Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! destructure_fields{
- ($matched:expr; $($variant:path),+ => $value:ident) => (
- match $matched {
- $($variant($value))|+ => $value
- }
- );
- ($matched:expr; $($variant:path),+ => $($values:ident),+) => (
- destructure_fields!(@create_match $matched, $($variant),+ => ($($values),+))
- );
- (@create_tuple ($($values:ident),+)) => {
- ($($values),+)
- };
- (@create_pattern $variant:path, ($($values:ident),+)) => {
- $variant($($values),+)
- };
- (@create_match $matched:expr, $($variant:path),+ => $value_tuple:tt) => (
- match $matched {
- $(destructure_fields!(@create_pattern $variant, $value_tuple) => (destructure_fields!(@create_tuple $value_tuple))),+
- }
- );
- }
- enum Test<'a> {
- A(&'a str, usize),
- B(&'a str, usize),
- C(&'a str, usize),
- D(&'a str, usize)
- }
- impl<'a> Test<'a> {
- fn get_content(&self) -> (&str, &usize) {
- destructure_fields!(self; Test::A, Test::B, Test::C, Test::D => string, number)
- }
- }
- fn main() {
- let (string, number) = Test::B("This is a test!", 5).get_content();
- println!("{}{}", string, number);
- }
Add Comment
Please, Sign In to add comment