Graeme
Graeme

Reputation: 427

A macro with quotes around a metavariable

I want to println! a macro identifier, (mostly for testing macros), but if I do it like this:

macro_rules! printname(
(
    $name:ident;
) => (
    println!("$name");
)

let this_name = "hello";

printname!(this_name);

it just prints

$name

and if i do println!("{}", $name); it of course substitutes in the variable this_name and prints

hello

What I actually want it to do is print

this_name

Is there a way to quote a metavariable to get its value?

Upvotes: 7

Views: 3082

Answers (1)

Mark Rousskov
Mark Rousskov

Reputation: 971

The stringify! macro should work. It provides a string representation of the passed argument as a &'static str, which can then be passed directly into println! to print it to stdout.

Upvotes: 9

Related Questions