Declarative macro : either list of elements separated by comma or tuple?

How to generate either list of elements separated by comma or tuple?

Best I can get is tuple of tuple of tuple ..., but that is not what needed

[src/main.rs:48] b = (
    10,
    (
      20,
      30,
    ),
)

Would be great to get:

[src/main.rs:48] b = (
    10,
    20,
    30,
)

Playground.

  ( $v : expr ) =>
  {
    f1( $v )
  };

  ( $( $rest : expr ),+ ) =>
  {(
    $( val!( $rest ) ),+
  )};

playground

1 Like

That works! :smiley:

What if goal is getting comma separated list instead of tuple?

  ( $( $rest : expr ),+ ) =>
  {
    $( val!( $rest ) ),+,
  };

This does not work.

Playground

According to tlborm:

Expansion - The Little Book of Rust Macros

syntax extension absolutely cannot expand to incomplete or syntactically invalid constructs

2 Likes

I see. It was difficult to me to draw this conclusion from that page. Thank you for help @vague !