I am using a macro to generate a series of enum variants. I would like to add a bit of documentation to each one, which I can do with the #[doc = "..."]
attribute. This works fine if there's a string literal, but each macro variant has some additional data that I would like to put into the doc string.
In ordinary code, I would use format!
to produce a formatted string. Is there any way to do this in a macro?
Here's a minimum example. It tries to generate a three-variant enum with a doc attribute on each variant. If the format!
macro is replaced with a string literal, this works just fine.
macro_rules! fancy_enum {
($($name:ident => ($str:expr, $int:expr)),+) => {
pub enum Fancy {
$(
#[doc = format!("Variant with string {} and value {}", $str, $int)]
$name
),+
}
}
}
fancy_enum!(
A => ("alpha", 1),
B => ("bravo", 2),
C => ("charlie", 3)
);