知识卡片
记号转字符串不求值
stringify!(1 + 2) 得到源代码记号的字符串形式,类似 1 + 2。格式化宏使用表达式求值后的结果。派生宏把类型名插入 stringify! 时,生成程序直接持有编译期字符串字面量,适合报告声明中的名字,无需运行时反射或用求值结果再构造字符串。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 高级特性 Procedural Macros for Generating Code from Attributes · 查看原书依据
The stringify! macro used here is built into Rust. It takes a Rust expression, such as 1 + 2, and at compile time turns the expression into a string literal, such as "1 + 2". This is different from format! or println!, macros which evaluate the expression and then turn the result into a String.
参考来源
- 位置:《The Rust Programming Language(第2版)》第19章 高级特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c19.xhtml`,块 0400)
- 结论依据:原文以 1 + 2 对比 stringify! 与 format!、println!,同时说明编译期生成字面量的效果。
- 原始内容:The stringify! macro used here is built into Rust. It takes a Rust expression, such as 1 + 2, and at compile time turns the expression into a string literal, such as "1 + 2". This is different from format! or println!, macros which evaluate the expression and then turn the result into a String.