10.3.1. 属性宏 (Attribute-like macros) Rust 过程宏 (Procedural Macros) 详解:属性宏 (Attribute-like Macros) 属性宏 (Attribute-like Macros) 概述 属性宏是过程宏的一种,它以属性 (attribute) 的形式附加到 Rust 代码项上,例如结构体 (struct)、枚举 (enum)、函数 (function) 等。属性宏在编译时被调用,接收被附加代码项的语法树 (Abstract Syntax Tree, AST) 作为输入,并可以修改或替换该代码项,最终生成新的 Rust 代码。 与派生宏类似,属性宏也用于代码生成,但它们的应用场景更加广泛和灵活。