Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! destructure_field{
- ($matched:expr; $($variant:path),+ => $value:ident) => (
- match $matched {
- $($variant($value))|+ => $value
- }
- );
- // Problematic variant
- ($matched:expr; $($variant:path),+ => $($values:ident),+) => (
- match $matched {
- // nested repetition occurs here
- $($variant($($values),+))|+ => ($($values),+)
- }
- )
- }
- 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_field!(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