Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
df086575
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
df086575
编写于
2月 20, 2015
作者:
K
Keegan McAllister
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tweak wording in the macros guide
上级
1804242a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
19 addition
and
10 deletion
+19
-10
src/doc/trpl/macros.md
src/doc/trpl/macros.md
+19
-10
未找到文件。
src/doc/trpl/macros.md
浏览文件 @
df086575
...
...
@@ -189,14 +189,12 @@ shorthand for a data type could be valid as either an expression or a pattern.
## Repetition
The repetition behavior can seem somewhat magical, especially when multiple
names are bound at multiple nested levels of repetition. The two rules to keep
in mind are:
The repetition operator follows two principal rules:
1.
the behavior of
`$(...)*`
is to walk through one "layer" of repetitions, for
all of the
`$name`
s
it contains, in lockstep, and
1.
`$(...)*`
walks through one "layer" of repetitions, for all of the
`$name`
s
it contains, in lockstep, and
2.
each
`$name`
must be under at least as many
`$(...)*`
s as it was matched
against. If it is under more, it'll be duplicated, as appropriate.
against. If it is under more, it'll be duplicated, as appropriate.
This baroque macro illustrates the duplication of variables from outer
repetition levels.
...
...
@@ -226,6 +224,10 @@ That's most of the matcher syntax. These examples use `$(...)*`, which is a
more" match. Both forms optionally include a separator, which can be any token
except
`+`
or
`*`
.
This system is based on
"
[
Macro-by-Example
](
http://www.cs.indiana.edu/ftp/techreports/TR206.pdf
)
"
(PDF link).
# Hygiene
Some languages implement macros using simple text substitution, which leads to
...
...
@@ -273,19 +275,26 @@ macro, using [a GNU C extension] to emulate Rust's expression blocks.
})
```
This looks reasonable, but watch what happens in this example
:
Here's a simple use case that goes terribly wrong
:
```
text
const char *state = "reticulating splines";
LOG(state)
;
LOG(state)
```
Th
e program will likely segfault, after it tries to execute
Th
is expands to
```
text
printf("log(%d): %s\n", state, state);
const char *state = "reticulating splines";
int state = get_log_state();
if (state > 0) {
printf("log(%d): %s\n", state, state);
}
```
The second variable named
`state`
shadows the first one. This is a problem
because the print statement should refer to both of them.
The equivalent Rust macro has the desired behavior.
```
rust
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录