From d3e8729a0773df492c1b28d65588cc03a72748a8 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Sat, 22 Sep 2018 13:48:55 -0500 Subject: [PATCH 1/2] Edition notes about --- src/SUMMARY.md | 1 + src/rust-2018/macros/at-most-once.md | 38 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/rust-2018/macros/at-most-once.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 5623a0b5..aaa68c05 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -53,6 +53,7 @@ - [Macros](rust-2018/macros/index.md) - [Custom Derive](rust-2018/macros/custom-derive.md) - [Macro changes](rust-2018/macros/macro-changes.md) + - [At most one repetition](rust-2018/macros/at-most-once.md) - [The compiler](rust-2018/the-compiler/index.md) - [Improved error messages](rust-2018/the-compiler/improved-error-messages.md) - [Incremental Compilation for faster compiles](rust-2018/the-compiler/incremental-compilation-for-faster-compiles.md) diff --git a/src/rust-2018/macros/at-most-once.md b/src/rust-2018/macros/at-most-once.md new file mode 100644 index 00000000..26d13036 --- /dev/null +++ b/src/rust-2018/macros/at-most-once.md @@ -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: + +```rust +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: + +```rust +macro_rules! foo { + ($a:ident $(, $b:expr)?) => { + println!("{}", $a); + + $( + println!("{}", $b); + )? + } +} +``` From ab2c46c85c8ae11cd38fbc9e6feba4108265b75a Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Mon, 3 Dec 2018 13:01:28 -0600 Subject: [PATCH 2/2] rust 2018 --- src/rust-2018/macros/at-most-once.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rust-2018/macros/at-most-once.md b/src/rust-2018/macros/at-most-once.md index 26d13036..fb38610d 100644 --- a/src/rust-2018/macros/at-most-once.md +++ b/src/rust-2018/macros/at-most-once.md @@ -8,7 +8,7 @@ In Rust 2018, we have made a couple of changes to the macros-by-example syntax. For example, consider the following Rust 2015 code: -```rust +```rust2018 macro_rules! foo { ($a:ident, $b:expr) => { println!("{}", $a); @@ -25,7 +25,7 @@ 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: -```rust +```rust2018 macro_rules! foo { ($a:ident $(, $b:expr)?) => { println!("{}", $a);