gpt4 book ai didi

rust - Rust宏: unit type?

转载 作者:行者123 更新时间:2023-12-03 11:45:02 29 4
gpt4 key购买 nike

以下宏:

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com