I want to expand a token to a string. For example, I have this macro:
macro String {
rule {
$x
} => {
"$x"
}
}
I would expect String 1
to expand to "1"
, however it expands to just 1;
How can I accomplish this?
EDIT: This seems imposible to do with a declarative approach, but should be possible with an imperative approach (see this comment):
macro String {
case {_ $x } => {
return #{"$x"}
}
}
But that still expands with quotes.
As noted in this issue thread, you can do it with an imperative approach, but it's a bit awkward and not well-documented. Basically, you do it like this:
BTW, I wouldn't call this macro "String" — it conflicts with the existing String that's natively part of JavaScript.