知识卡片
宏输出须匹配语法位置
宏的展开会出现在调用处。把 let a = my_vec!(1); 的宏调用替成创建变量、推入元素、返回向量三段代码时,赋值右侧需要一个表达式。将这些内容包进块,并保留尾部的 v 作为值,整个块就可用于赋值。转写器自身的外层定界符为宏语法所有,要输出块需再写一对大括号。
相关卡片 [[首条匹配决定展开]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 片段与块表达式
原书出处 · 1 处
- Declarative macros 片段与块表达式 · 查看原书依据
The reason becomes clear once you try to mentally substitute the macro call with its output. Take this example, which should match our current rule: let a_number_vec = my_vec!(1);. We know that my_vec!(1) will be replaced with the content of the transcriber. So since let a_number_vec = will stay in place, we need something that can be assigned to a let—say, an expression.
参考来源
- 位置:《Write Powerful Rust Macros》第02章《Declarative macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-2.xhtml#p51)
- 结论依据:my_vec 示例通过错误和成功形式解释两层界定符。
- 原始内容:The reason becomes clear once you try to mentally substitute the macro call with its output. Take this example, which should match our current rule: let a_number_vec = my_vec!(1);. We know that my_vec!(1) will be replaced with the content of the transcriber. So since let a_number_vec = will stay in place, we need something that can be assigned to a let—say, an expression.