-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from mark-i-m/macro_at_most_once_rep
Edition notes about `?`
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# At most one repetition | ||
|
||
In Rust 2018, we have made a couple of changes to the macros-by-example syntax. | ||
|
||
1. We have added a new Kleene operator `?` which means "at most one" | ||
repetition. This operator does not accept a separator token. | ||
2. We have disallowed using `?` as a separator to remove ambiguity with `?`. | ||
|
||
For example, consider the following Rust 2015 code: | ||
|
||
```rust2018 | ||
macro_rules! foo { | ||
($a:ident, $b:expr) => { | ||
println!("{}", $a); | ||
println!("{}", $b); | ||
} | ||
($a:ident) => { | ||
println!("{}", $a); | ||
} | ||
} | ||
``` | ||
|
||
Macro `foo` can be called with 1 or 2 arguments; the second one is optional, | ||
but you need a whole other matcher to represent this possibility. This is | ||
annoying if your matchers are long. In Rust 2018, one can simply write the | ||
following: | ||
|
||
```rust2018 | ||
macro_rules! foo { | ||
($a:ident $(, $b:expr)?) => { | ||
println!("{}", $a); | ||
$( | ||
println!("{}", $b); | ||
)? | ||
} | ||
} | ||
``` |