← 返回起始笔记位置

知识卡片

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

嵌入字面量可用静态切片

理解等级:中级

过程宏内部读取的字符串通常由文件解析结果持有。书中的生成器把它们插入 token 流时,生成代码得到新的字符串字面量。字面量在目标程序中具有静态生命周期,因而可把结果映射定义为 HashMap<&'static str, &'static str>,直接插入生成的字面量。

这改变了调用方拿到的数据类型和所有权。若需要修改字符串内容或向映射写入生命周期短于程序的值,String 仍是不同的接口选择。

相关卡片 [[编译期嵌入配置值]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第10章《Macros and the outside world》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-10.xhtml#p48) - 结论依据:原书明确说明所引用的是生成代码中的字面量,因此可以使用静态切片,并移除生成的 to_string 调用。 - 原始内容:But just for this once, let’s consider string slices, since they are a valid alternative and not that difficult to use in this particular example. After all, we are generating a new method that has the hardcoded values baked in.