Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Per The Rust Reference,

The character $ cannot be matched or transcribed literally.

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading