@@ -73,7 +73,7 @@ macro_rules! vec {
7373 };
7474}
7575# fn main () {
76- # assert_eq! (& [1 ,2 ,3 ], & vec! [1 ,2 ,3 ]);
76+ # assert_eq! ([1 ,2 ,3 ], vec! [1 ,2 ,3 ]);
7777# }
7878```
7979
@@ -189,14 +189,12 @@ shorthand for a data type could be valid as either an expression or a pattern.
189189
190190## Repetition
191191
192- The repetition behavior can seem somewhat magical, especially when multiple
193- names are bound at multiple nested levels of repetition. The two rules to keep
194- in mind are:
192+ The repetition operator follows two principal rules:
195193
196- 1 . the behavior of ` $(...)* ` is to walk through one "layer" of repetitions, for
197- all of the ` $name ` s it contains, in lockstep, and
194+ 1 . ` $(...)* ` walks through one "layer" of repetitions, for all of the ` $name ` s
195+ it contains, in lockstep, and
1981962 . each ` $name ` must be under at least as many ` $(...)* ` s as it was matched
199- against. If it is under more, it'll be duplicated, as appropriate.
197+ against. If it is under more, it'll be duplicated, as appropriate.
200198
201199This baroque macro illustrates the duplication of variables from outer
202200repetition levels.
@@ -226,6 +224,10 @@ That's most of the matcher syntax. These examples use `$(...)*`, which is a
226224more" match. Both forms optionally include a separator, which can be any token
227225except ` + ` or ` * ` .
228226
227+ This system is based on
228+ "[ Macro-by-Example] ( http://www.cs.indiana.edu/ftp/techreports/TR206.pdf ) "
229+ (PDF link).
230+
229231# Hygiene
230232
231233Some languages implement macros using simple text substitution, which leads to
@@ -273,19 +275,26 @@ macro, using [a GNU C extension] to emulate Rust's expression blocks.
273275})
274276```
275277
276- This looks reasonable, but watch what happens in this example :
278+ Here's a simple use case that goes terribly wrong :
277279
278280``` text
279281const char *state = "reticulating splines";
280- LOG(state);
282+ LOG(state)
281283```
282284
283- The program will likely segfault, after it tries to execute
285+ This expands to
284286
285287``` text
286- printf("log(%d): %s\n", state, state);
288+ const char *state = "reticulating splines";
289+ int state = get_log_state();
290+ if (state > 0) {
291+ printf("log(%d): %s\n", state, state);
292+ }
287293```
288294
295+ The second variable named ` state ` shadows the first one. This is a problem
296+ because the print statement should refer to both of them.
297+
289298The equivalent Rust macro has the desired behavior.
290299
291300``` rust
@@ -357,6 +366,64 @@ fn main() {
357366
358367[ items ] : ../reference.html#items
359368
369+ # Recursive macros
370+
371+ A macro's expansion can include more macro invocations, including invocations
372+ of the very same macro being expanded. These recursive macros are useful for
373+ processing tree-structured input, as illustrated by this (simplistic) HTML
374+ shorthand:
375+
376+ ``` rust
377+ # #![allow(unused_must_use)]
378+ macro_rules! write_html {
379+ ($ w : expr , ) => (());
380+
381+ ($ w : expr , $ e : tt ) => (write! ($ w , " {}" , $ e ));
382+
383+ ($ w : expr , $ tag : ident [ $ ($ inner : tt )* ] $ ($ rest : tt )* ) => {{
384+ write! ($ w , " <{}>" , stringify! ($ tag ));
385+ write_html! ($ w , $ ($ inner )* );
386+ write! ($ w , " </{}>" , stringify! ($ tag ));
387+ write_html! ($ w , $ ($ rest )* );
388+ }};
389+ }
390+
391+ fn main () {
392+ # // FIXME(#21826)
393+ use std :: fmt :: Write ;
394+ let mut out = String :: new ();
395+
396+ write_html! (& mut out ,
397+ html [
398+ head [title [" Macros guide" ]]
399+ body [h1 [" Macros are the best!" ]]
400+ ]);
401+
402+ assert_eq! (out ,
403+ " <html><head><title>Macros guide</title></head>\
404+ <body><h1>Macros are the best!</h1></body></html>" );
405+ }
406+ ```
407+
408+ # Debugging macro code
409+
410+ To see the results of expanding macros, run ` rustc --pretty expanded ` . The
411+ output represents a whole crate, so you can also feed it back in to ` rustc ` ,
412+ which will sometimes produce better error messages than the original
413+ compilation. Note that the ` --pretty expanded ` output may have a different
414+ meaning if multiple variables of the same name (but different syntax contexts)
415+ are in play in the same scope. In this case ` --pretty expanded,hygiene ` will
416+ tell you about the syntax contexts.
417+
418+ ` rustc ` provides two syntax extensions that help with macro debugging. For now,
419+ they are unstable and require feature gates.
420+
421+ * ` log_syntax!(...) ` will print its arguments to standard output, at compile
422+ time, and "expand" to nothing.
423+
424+ * ` trace_macros!(true) ` will enable a compiler message every time a macro is
425+ expanded. Use ` trace_macros!(false) ` later in expansion to turn it off.
426+
360427# Further reading
361428
362429The [ advanced macros chapter] [ ] goes into more detail about macro syntax. It
0 commit comments