结构字段名中的Rust宏参数
我想在宏中获取字段名的一部分,然后将其与另一部分连接起来使用 下面是C语言中的一个示例:结构字段名中的Rust宏参数,rust,macros,Rust,Macros,我想在宏中获取字段名的一部分,然后将其与另一部分连接起来使用 下面是C语言中的一个示例: #define MY_MACRO(NAME) cout << mystruct->foo_##NAME << endl; 但我明白了: error: expected one of `(`, `,`, `.`, `::`, `?`, or an operator, found `!` --> macrotest.rs:12:46 | 12 |
#define MY_MACRO(NAME) cout << mystruct->foo_##NAME << endl;
但我明白了:
error: expected one of `(`, `,`, `.`, `::`, `?`, or an operator, found `!`
--> macrotest.rs:12:46
|
12 | println!("{}", mystruct.concat_idents!(foo_, $NAME));
| ^ expected one of `(`, `,`, `.`, `::`, `?`, or an operator
...
16 | MY_MACRO!(bar);
| --------------- in this macro invocation
|
这回答了你的问题吗@塔德曼:是的,但事实上,你能解释得再多一点吗?@tadman我编辑了这个问题,现在更具体了…
concat#s
是一个宏,必须单独使用。它不能像函数调用一样使用。试试:concat\n标识!(mystruct.foo_,$NAME)
尽量不要用C语言来理解这一点,因为预处理器可以扩展。Rust的宏要比这优雅得多。
error: expected one of `(`, `,`, `.`, `::`, `?`, or an operator, found `!`
--> macrotest.rs:12:46
|
12 | println!("{}", mystruct.concat_idents!(foo_, $NAME));
| ^ expected one of `(`, `,`, `.`, `::`, `?`, or an operator
...
16 | MY_MACRO!(bar);
| --------------- in this macro invocation
|