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 5353 - [ Macros] ( rust-2018/macros/index.md )
5454 - [ Custom Derive] ( rust-2018/macros/custom-derive.md )
5555 - [ Macro changes] ( rust-2018/macros/macro-changes.md )
56+ - [ At most one repetition] ( rust-2018/macros/at-most-once.md )
5657 - [ The compiler] ( rust-2018/the-compiler/index.md )
5758 - [ Improved error messages] ( rust-2018/the-compiler/improved-error-messages.md )
5859 - [ 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