【发布时间】:2020-08-10 02:31:46
【问题描述】:
以下宏:
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`.
我特别想将unit type 用于其中一些枚举情况,根据定义,它们应该是有效类型。我将如何做到这一点?
【问题讨论】:
-
随机猜测
(()) -
试过了;它表现为
Foo(())。这也许可行,但远非理想。 -
但是单位类型是
(),所以单位类型的变体是Foo(())。也许你想要一个空的类似元组的枚举? -
是的,理想的结果实际上只是
Foo,但我可以接受Foo()