Skip to content

Commit c5eb0c9

Browse files
committed
Edition notes about
1 parent 3c181d2 commit c5eb0c9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
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)

src/rust-2018/macros/at-most-once.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
```rust
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+
```rust
29+
macro_rules! foo {
30+
($a:ident $(, $b:expr)?) => {
31+
println!("{}", $a);
32+
33+
$(
34+
println!("{}", $b);
35+
)?
36+
}
37+
}
38+
```

0 commit comments

Comments
 (0)