Skip to content

Commit a96a8b2

Browse files
author
Keegan McAllister
committed
Update macro scope intro
1 parent 128e7ff commit a96a8b2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/doc/trpl/macros.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,18 @@ to print "I am never printed" and to run forever.
440440

441441
# Scoping and macro import/export
442442

443-
Macros occupy a single global namespace. The interaction with Rust's system of
444-
modules and crates is somewhat complex.
443+
Macros are expanded at an early stage in compilation, before name resolution.
444+
One downside is that scoping works differently for macros, compared to other
445+
constructs in the language.
445446

446447
Definition and expansion of macros both happen in a single depth-first,
447448
lexical-order traversal of a crate's source. So a macro defined at module scope
448449
is visible to any subsequent code in the same module, which includes the body
449450
of any subsequent child `mod` items.
450451

452+
A macro defined within the body of a single `fn`, or anywhere else not at
453+
module scope, is visible only within that item.
454+
451455
If a module has the `macro_use` attribute, its macros are also visible in its
452456
parent module after the child's `mod` item. If the parent also has `macro_use`
453457
then the macros will be visible in the grandparent after the parent's `mod`

0 commit comments

Comments
 (0)