rust - Rust宏: unit type?

标签 rust macros

以下宏:

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/

相关文章:

file - 如何检查给定路径是文件还是目录?

Docker 和 "The OpenSSL library reported an error"部署时

c - 指示MAC地址长度的标准c宏?

c - C offsetof 宏是如何工作的?

visual-studio - 通过DTE获取项目TargetPath的宏值

c - 当函数有整数作为参数时,如何传递字符串作为参数?

android - 何时在 Android NDK 中使用 JNIEXPORT 和 JNICALL?

build - 如何动态启用 crate 功能? [复制]

multithreading - MUD 服务器的 Rust 同步策略

rust - 如何定义外部类型?