Index tuple struct with macro_rules input

Is it possible to use input from a macro to index into a tuple struct?

For example, you can do


struct MyStruct {
  ...
}

macro_rules index {
    ($field:ident) => {
        MyStruct.$field
    }
}

Basically, is there a way to do index!(0) to index into a tuple struct??

it's doable, but not very robust, when you give it invalid input, the error message might be confusing(typically "unexpected token XXX").

the trick is you can use the "catch-all" tt capture, the literal capture doesn't work, because it also captures other literal tokens, while the tuple index require a specific decimal integer literal token.

macro_rules! index {
    ($v:expr, $i:tt) => {
        $v . $i
    }
}
let tuple = (42, 'a', "abc");
let x = index!(tuple, 0);
let y = index!(tuple, 1);
let z = index!(tuple, 2);