Replacing struct constructor in macro_rules!

I want to replace constructor name from inside a macro:

let _ = TO_REPLACE { };

In general tt can be used, but I want to pass paths (with ::) to it, and with path designator this does not compile.

Any way I can resolve this?

Here is an example on Rust Playground

If you add an extra set of {} inside build! like below

macro_rules! build {
    ($ty:path) => {{
        $ty { a: 10 }
    }};
}

to make it a block expression, that would work.

1 Like

For me this is a part of a bigger macro, which also does some struct definitions.
So I don't think I will be able to use block expressions there?

You can also try this:

macro_rules! build {
    ($($ty:tt)+) => {
        $($ty)+ { a: 10 }
    };
}

In your playground it worked.

1 Like

This works, thank you!