Skip to content

Commit 9a90151

Browse files
ehusstraviscross
authored andcommitted
Add some more explicit macro_export rules
This adds more rules to be a little more explicit about the behavior, along with some examples.
1 parent f873453 commit 9a90151

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/macros-by-example.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,6 @@ r[macro.decl.scope.macro_export]
374374
r[macro.decl.scope.macro_export.intro]
375375
The *`macro_export` [attribute][attributes]* marks a macro to be publicly exported from the crate, and makes it available in the root of the crate for path-based resolution.
376376

377-
378-
r[macro.decl.scope.path.export]
379-
Macros labeled with `#[macro_export]` are always `pub` and can be referred to by other crates, either by path or by `#[macro_use]` as described above.
380377
> [!EXAMPLE]
381378
> ```rust
382379
> self::m!();
@@ -410,6 +407,53 @@ Only the first instance of `macro_export` on a macro is honored. Subsequent `mac
410407
> [!NOTE]
411408
> `rustc` currently warns on subsequent duplicate `macro_export` attributes.
412409
410+
r[macro.decl.scope.macro_export.path-based]
411+
By default, macros only have [textually-based scoping](#textual-scope). When the `macro_export` attribute is used, the macro is reexported in the crate root, and can be referred to using a path to the macro in the crate root.
412+
413+
r[macro.decl.scope.macro_export.export]
414+
The `macro_export` attribute causes a macro to be publicly exported from the crate root so that it can be referred to by other crates using a path.
415+
416+
> [!EXAMPLE]
417+
> Given the following defined in a crate named `log`:
418+
> ```rust
419+
> #[macro_export]
420+
> macro_rules! warn {
421+
> ($message:expr) => { eprintln!("WARN: {}", $message) };
422+
> }
423+
> ```
424+
> Then you can refer to the macro via a path to the crate:
425+
> <!-- ignore: requires external crates -->
426+
> ```rust,ignore
427+
> fn main() {
428+
> log::warn!("example warning");
429+
> }
430+
> ```
431+
432+
r[macro.decl.scope.macro_export.macro_use]
433+
`macro_export` also allows the use of [`macro_use`][macro.decl.scope.macro_use] on an `extern crate` to import the macro into the [`macro_use` prelude].
434+
435+
> [!EXAMPLE]
436+
> Given the following defined in a crate named `log`:
437+
> ```rust
438+
> #[macro_export]
439+
> macro_rules! warn {
440+
> ($message:expr) => { eprintln!("WARN: {}", $message) };
441+
> }
442+
> ```
443+
> Using `macro_use` in a dependent crate means the macro can be used from the prelude:
444+
> <!-- ignore: requires external crates -->
445+
> ```rust,ignore
446+
> #[macro_use]
447+
> extern crate log;
448+
>
449+
> pub mod util {
450+
> pub fn do_thing() {
451+
> // Resolved via macro prelude.
452+
> warn!("example warning");
453+
> }
454+
> }
455+
> ```
456+
413457
r[macro.decl.hygiene]
414458
## Hygiene
415459

0 commit comments

Comments
 (0)