知识卡片
过程宏需要消费端
过程宏入口定义在 proc-macro crate 的根模块。派生在函数上用 #[proc_macro_derive(Hello)] 声明外部宏名,函数名可独立命名。应用作为不同 crate 引用宏库,即使二者由同一个目录或工作区管理也是如此。推荐调用点用 use hello_world_macro::Hello 明确引入;分析历史示例时还会见到 #[macro_use] extern crate。
相关卡片 [[生成代码要在调用端验证]]、[[过程宏的三种接口]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- A “Hello, World” procedural macro 建立派生宏与消费端 · 查看原书依据
We have to modify the generated Cargo.toml. The most important change is adding a lib section, with the proc-macro property set to true. This tells Rust that this library will expose one or more procedural macros and will make some tooling available to us.
参考来源
- 位置:《Write Powerful Rust Macros》第03章《A “Hello, World” procedural macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-3.xhtml#p17)
- 结论依据:书中先以本地依赖连接宏库与应用,继而区分旧式导入和 use。
- 原始内容:We have to modify the generated Cargo.toml. The most important change is adding a lib section, with the proc-macro property set to true. This tells Rust that this library will expose one or more procedural macros and will make some tooling available to us.