以下宏:
macro_rules! generate_parse_expression_ast_data {
($lit:literal) => ();
}
enum Ast {
Foo (generate_parse_expression_ast_data!("bar")),
}
给出此错误:error: macro expansion ends with an incomplete expression: expected type
--> src/main.rs:6:10
|
2 | ($lit:literal) => ();
| -- in this macro arm
...
6 | Foo (generate_parse_expression_ast_data!("bar")),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected type
| this macro call doesn't expand to a type
error: aborting due to previous error
error: could not compile `playground`.
Playground link我特别想在某些枚举情况下使用unit type,根据定义,这应该是有效的类型。我将如何完成?
最佳答案
看起来像known issue。
我现在最好的建议是,除了发出零大小的类型(如单位)外,还应该重组宏,以便它完整地生成变体。例如:
macro_rules! generate_parse_expression_ast_data {(
enum $name:ident {
$( $variant:ident($lit:literal), )+
}
) => (
enum $name {
$( $variant(), )+
}
)}
generate_parse_expression_ast_data! {
enum Ast {
Foo("bar"),
}
}
关于rust - Rust宏: unit type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63333366/