rustrust-macros

Escape $ dollar sign in macro_rules


How can I escape a dollar sign for a macro like this?

macro_rules! test {
    ($ $name:ident) => {
        println!(stringify!($name));
    };
}

fn main() {
    test!($abc);
}

I want to get abc. I've tried using $$ and a bunch of possible ways to escape it but I can't find anything. All I can find on this online is to use $$ to escape meta variables and produces an error. Can I use the literal type somehow?

Here is what I have tried: $$ \$ _$ $_$ $:literal $$:literal


Solution

  • Per The Rust Reference,

    The character $ cannot be matched or transcribed literally.

    https://doc.rust-lang.org/reference/macros-by-example.html#transcribing