< 返回版块

mqxqls 发表于 2021-10-16 09:28

    #[stable(feature = "rust1", since = "1.0.0")]

我想知道这些宏的意思是指什么?暂时不需知道原理,有时候看到其他依赖都这样的写法,还有更多这样不同宏的意思吗?

评论区

写评论
苦瓜小仔 2021-10-16 12:53

需要 知道的一切都在这:过程宏 procedural macros

很重要的几句话:

Procedural macros allow creating syntax extensions as execution of a function. Procedural macros come in one of three flavors:

Function-like macros - custom!(...) Derive macros - #[derive(CustomDerive)] Attribute macros - #[CustomAttribute]

Procedural macros allow you to run code at compile time that operates over Rust syntax, both consuming and producing Rust syntax. You can sort of think of procedural macros as functions from an AST to another AST.

总之一句话,宏(指过程宏和声明宏)就是在编译时执行和生成 Rust 语法(是 AST 层面的操作),目的之一是用简洁的额外(甚至自定义)语法做很多重复(或者复杂)的事。

至于你看到的这个宏语法,属于过程宏的第三种属性宏。

1 共 1 条评论, 1 页