知识卡片
名字符号与字符串
在宏里,用标识符插值能让生成的 impl 正确指向输入类型。要把名字打印出来,可让 to_string 先产生字符串值再插值,或用 stringify! 将令牌文本转换为字符串。一个类型可以有多个 impl 块,但同签名位置的重复固有方法名会冲突,所以生成名字也属于宏 API 的维护范围。
相关卡片 [[解析与插值生成实现]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 练习与总结
原书出处 · 1 处
- A “Hello, World” procedural macro 练习与总结 · 查看原书依据
4. See if you can output a greeting followed by the name of the input (e.g., “Hello Example”). Fair warning: passing #name to print will not be enough, because that’s an identifier, and you need a string.
参考来源
- 位置:《Write Powerful Rust Macros》第03章《A “Hello, World” procedural macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-3.xhtml#p99)
- 结论依据:练习明确给出名字转文本的两种路线;前文实验证实自动生成方法名可能与用户方法冲突。
- 原始内容:4. See if you can output a greeting followed by the name of the input (e.g., “Hello Example”). Fair warning: passing #name to print will not be enough, because that’s an identifier, and you need a string.