File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 53
53
- [ Macros] ( rust-2018/macros/index.md )
54
54
- [ Custom Derive] ( rust-2018/macros/custom-derive.md )
55
55
- [ Macro changes] ( rust-2018/macros/macro-changes.md )
56
+ - [ At most one repetition] ( rust-2018/macros/at-most-once.md )
56
57
- [ The compiler] ( rust-2018/the-compiler/index.md )
57
58
- [ Improved error messages] ( rust-2018/the-compiler/improved-error-messages.md )
58
59
- [ Incremental Compilation for faster compiles] ( rust-2018/the-compiler/incremental-compilation-for-faster-compiles.md )
Original file line number Diff line number Diff line change
1
+ # At most one repetition
2
+
3
+ In Rust 2018, we have made a couple of changes to the macros-by-example syntax.
4
+
5
+ 1 . We have added a new Kleene operator ` ? ` which means "at most one"
6
+ repetition. This operator does not accept a separator token.
7
+ 2 . We have disallowed using ` ? ` as a separator to remove ambiguity with ` ? ` .
8
+
9
+ For example, consider the following Rust 2015 code:
10
+
11
+ ``` rust2018
12
+ macro_rules! foo {
13
+ ($a:ident, $b:expr) => {
14
+ println!("{}", $a);
15
+ println!("{}", $b);
16
+ }
17
+ ($a:ident) => {
18
+ println!("{}", $a);
19
+ }
20
+ }
21
+ ```
22
+
23
+ Macro ` foo ` can be called with 1 or 2 arguments; the second one is optional,
24
+ but you need a whole other matcher to represent this possibility. This is
25
+ annoying if your matchers are long. In Rust 2018, one can simply write the
26
+ following:
27
+
28
+ ``` rust2018
29
+ macro_rules! foo {
30
+ ($a:ident $(, $b:expr)?) => {
31
+ println!("{}", $a);
32
+
33
+ $(
34
+ println!("{}", $b);
35
+ )?
36
+ }
37
+ }
38
+ ```
You can’t perform that action at this time.
0 commit comments