Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 843
Jack Avatar asked Nov 30 '25 11:11

Jack


1 Answers

Per The Rust Reference,

The character $ cannot be matched or transcribed literally.

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

like image 96
Samathingamajig Avatar answered Dec 03 '25 00:12

Samathingamajig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!