Skip to content

Commit 65679b0

Browse files
author
Keegan McAllister
committed
---
yaml --- r: 187783 b: refs/heads/tmp c: 1804242 h: refs/heads/master i: 187781: 9d7ccb3 187779: 0dd26e2 187775: 0340a0c v: v3
1 parent bf5f507 commit 65679b0

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
3434
refs/heads/beta: 522d09dfecbeca1595f25ac58c6d0178bbd21d7d
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3636
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
37-
refs/heads/tmp: 65e1e6bb942701a061f6772507141ea9f8e920e2
37+
refs/heads/tmp: 1804242a2d4037fd3ae854989a97a65e16bda05a
3838
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/tmp/src/doc/trpl/advanced-macros.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,53 @@ To keep this system simple and correct, `#[macro_use] extern crate ...` may
192192
only appear at the root of your crate, not inside `mod`. This ensures that
193193
`$crate` is a single identifier.
194194

195+
# The deep end
196+
197+
The introductory chapter mentioned recursive macros, but it did not give the
198+
full story. Recursive macros are useful for another reason: Each recursive
199+
invocation gives you another opportunity to pattern-match the macro's
200+
arguments.
201+
202+
As an extreme example, it is possible, though hardly advisable, to implement
203+
the [Bitwise Cyclic Tag](http://esolangs.org/wiki/Bitwise_Cyclic_Tag) automaton
204+
within Rust's macro system.
205+
206+
```rust
207+
#![feature(trace_macros)]
208+
209+
macro_rules! bct {
210+
// cmd 0: d ... => ...
211+
(0, $($ps:tt),* ; $_d:tt)
212+
=> (bct!($($ps),*, 0 ; ));
213+
(0, $($ps:tt),* ; $_d:tt, $($ds:tt),*)
214+
=> (bct!($($ps),*, 0 ; $($ds),*));
215+
216+
// cmd 1p: 1 ... => 1 ... p
217+
(1, $p:tt, $($ps:tt),* ; 1)
218+
=> (bct!($($ps),*, 1, $p ; 1, $p));
219+
(1, $p:tt, $($ps:tt),* ; 1, $($ds:tt),*)
220+
=> (bct!($($ps),*, 1, $p ; 1, $($ds),*, $p));
221+
222+
// cmd 1p: 0 ... => 0 ...
223+
(1, $p:tt, $($ps:tt),* ; $($ds:tt),*)
224+
=> (bct!($($ps),*, 1, $p ; $($ds),*));
225+
226+
// halt on empty data string
227+
( $($ps:tt),* ; )
228+
=> (());
229+
}
230+
231+
fn main() {
232+
trace_macros!(true);
233+
# /* just check the definition
234+
bct!(0, 0, 1, 1, 1 ; 1, 0, 1);
235+
# */
236+
}
237+
```
238+
239+
Exercise: use macros to reduce duplication in the above definition of the
240+
`bct!` macro.
241+
195242
# A final note
196243

197244
Macros, as currently implemented, are not for the faint of heart. Even

0 commit comments

Comments
 (0)