有没有办法在这样的宏中提取 struct 字段的"索引"(出现的顺序):
macro_rules! example {
(struct $name:ident {
$($field_name:ident: $field_type:ty,)*
}) => {
struct $name {
$($field_name: $field_type,)*
}
impl $name {
fn print_members(&self) {
$(println!("{} {} {}",
stringify!($field_name),
stringify!($field_type),
stringify!(<<<FIELD INDEX SOMEHOW>>>),
);)*
}
}
}
}
以至于
example! {
struct SomeStruct {
a: String,
b: String,
c: usize,
}
}
将打印:
a String 0
b String 1
c usize 2
我非常肯定proc宏可以做到这一点(并不是我知道如何编写),但是使用声明性宏能做到这一点吗?