知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.1

声明宏作用域不同于普通项

理解等级:中级

macro_rules声明宏的文本使用通常需要定义在前;macro_export导出的宏名位于crate根,即使定义写在子模块里。路径导入与文本作用域有不同解析规则,找不到宏时应检查实际入口,不能照搬普通函数的模块路径推断。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 5. Tooling Declarative Macros · 查看原书依据

      macro_export] attribute makes a macro more widely visible, but this also has an oddity: a macro appears at the top level of a crate, even if it’s defined in a module: mod submod { #[macro_export] macro_rules! cube { { $e:expr } => { $e * $e * $e } } } mod user { pub fn use_macro() { // Note: *not* `crate::submod::cube!` let cubed = crate::cube!(3); println!(

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第5章《Declarative Macros》(spine9;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch05.html;u141-ch05-007)。 - 结论依据:原文在“macro_export”附近的定义、例子或代码支撑本卡核心说明:macro_rules声明宏的文本使用通常需要定义在前;macro_export导出的宏名位于crate根,即使定义写在子模块里。边界按已读前提解释;示例未执行。 - 原始内容:macro_export] attribute makes a macro more widely visible, but this also has an oddity: a macro appears at the top level of a crate, even if it’s defined in a module: mod submod { #[macro_export] macro_rules! cube { { $e:expr } => { $e * $e * $e } } } mod user { pub fn use_macro() { // Note: *not* `crate::submod::cube!` let cubed = crate::cube!(3); println!(