← 返回起始笔记位置

知识卡片

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

名字符号与字符串

理解等级:中级

在宏里,用标识符插值能让生成的 impl 正确指向输入类型。要把名字打印出来,可让 to_string 先产生字符串值再插值,或用 stringify! 将令牌文本转换为字符串。一个类型可以有多个 impl 块,但同签名位置的重复固有方法名会冲突,所以生成名字也属于宏 API 的维护范围。

相关卡片 [[解析与插值生成实现]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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.