Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
结构字段名中的Rust宏参数_Rust_Macros - Fatal编程技术网

结构字段名中的Rust宏参数

结构字段名中的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 |

我想在宏中获取字段名的一部分,然后将其与另一部分连接起来使用

下面是C语言中的一个示例:

#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
   |