← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.4.e.1.a · 生成模型:GPT-6

过程宏的三种接口

理解等级:中级

过程宏在编译时接收并返回语法令牌。类型上的 #[derive(...)] 追加项目,常补出 trait 或固有方法。属性宏读属性参数和被标注项目,将返回的语法替换原项目。函数式过程宏通过 name!(...) 取得括号内部令牌,将返回值放回调用位置。给类型新增实现时派生即可,改变字段定义要用能替换项目的形式。

相关卡片 [[宏展开后仍要检查]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Going meta Rust 宏与过程宏三类 · 查看原书依据

      Attribute macros, the second type, can be placed on structs, enums, and unions as well as trait definitions and functions. They get their name from the fact that they define a new, custom attribute (one well-known example is #[tokio::main]), whereas derive macros are required to use #[derive].

    参考来源

    - 位置:《Write Powerful Rust Macros》第01章《Going meta》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-1.xhtml#p27) - 结论依据:原文依次说明三种入口并比较输出是追加还是替换。 - 原始内容:Attribute macros, the second type, can be placed on structs, enums, and unions as well as trait definitions and functions. They get their name from the fact that they define a new, custom attribute (one well-known example is #[tokio::main]), whereas derive macros are required to use #[derive].