【发布时间】:2021-10-01 13:46:41
【问题描述】:
我正在尝试根据可选模式$( … )? 的存在来选择生成一个结构:
macro_rules! accounts_struct {
(
$( #[seeds: struct $StructSeedsName:ident] )?
struct $StructAccountsName:ident {
$(
pub $account:ident,
)*
}
) => {
pub struct $StructAccountsName {
$(
pub $account: String,
)*
}
$(
pub struct $StructSeedsName {
$(
pub $account: u8,
)*
}
)?
}
}
accounts_struct! {
#[seeds: struct Seeds]
struct Accounts {
pub foo,
pub bar,
}
}
很遗憾,我似乎不被允许这样做:
error: meta-variable `StructSeedsName` repeats 1 time, but `account` repeats 2 times
--> src/lib.rs:16:10
|
16 | $(
| __________^
17 | | pub struct $StructSeedsName {
18 | | $(
19 | | pub $account: u8,
20 | | )*
21 | | }
22 | | )?
| |_________^
我可以使用什么解决方法?
【问题讨论】:
-
这个问题是我的一个问题,但答案并不符合我的问题:stackoverflow.com/questions/61045284/…
-
如果我理解了这个问题,我必须将重复嵌套在扩展部分中,就像在声明部分中一样。