Guest User

Untitled

a guest
Jun 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. macro_rules! destructure_field{
  2. ($matched:expr; $($variant:path),+ => $value:ident) => (
  3. match $matched {
  4. $($variant($value))|+ => $value
  5. }
  6. );
  7. // Problematic variant
  8. ($matched:expr; $($variant:path),+ => $($values:ident),+) => (
  9. match $matched {
  10. // nested repetition occurs here
  11. $($variant($($values),+))|+ => ($($values),+)
  12. }
  13. )
  14. }
  15.  
  16. enum Test<'a> {
  17. A(&'a str, usize),
  18. B(&'a str, usize),
  19. C(&'a str, usize),
  20. D(&'a str, usize)
  21. }
  22.  
  23. impl<'a> Test<'a> {
  24. fn get_content(&self) -> (&str, usize) {
  25. destructure_field!(self; Test::A, Test::B, Test::C, Test::D => string, number)
  26. }
  27. }
  28.  
  29. fn main() {
  30. let (string, number) = Test::B("This is a test!", 5).get_content();
  31. println!("{}{}", string, number);
  32. }
Add Comment
Please, Sign In to add comment