知识卡片
文档注释解析成属性
Rust 在宏看到输入前,把文档注释转换成 #[doc = "..."] 或对应的内部属性。外部文档注释(如 ///)关联后续条目,内部文档注释(如 //!)关联所在条目。函数式宏可解析传给自己的这类语法。书中先用 Attribute::parse_outer 取得位于结构体声明之前的属性,再用 braced! 进入大括号内容,用 Attribute::parse_inner 读取内部属性。内容可从 Meta::NameValue 的字面量中提取。示例解析器处理的是自定义宏输入语法。
相关卡片 [[属性形状决定解析方法]]、[[解析器应决定剩余输入]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Builder with attributes 在函数式宏读取文档属性 · 查看原书依据
There’s nothing special when it comes to the four kinds of documentation attributes. In fact, as the documentation notes, comments are transformed into normal-looking attributes (#[doc = r"your comment text here"]) before macros enter the equation.
参考来源
- 位置:《Write Powerful Rust Macros》第08章《Builder with attributes》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-8.xhtml#p196)
- 结论依据:清单 8.40–8.41 展示函数式宏在输入中解析内外文档注释,原文给出 doc 名值属性节点作为证据。
- 原始内容:There’s nothing special when it comes to the four kinds of documentation attributes. In fact, as the documentation notes, comments are transformed into normal-looking attributes (#[doc = r"your comment text here"]) before macros enter the equation.