You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/macros-by-example.md
+70-30Lines changed: 70 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -326,47 +326,86 @@ fn foo() {
326
326
// m!(); // Error: m is not in scope.
327
327
```
328
328
329
+
<!-- template:attributes -->
329
330
r[macro.decl.scope.macro_use]
330
331
### The `macro_use` attribute
331
332
332
-
r[macro.decl.scope.macro_use.mod-decl]
333
-
The *`macro_use` attribute* has two purposes. First, it can be used to make a
334
-
module's macro scope not end when the module is closed, by applying it to a
335
-
module:
333
+
r[macro.decl.scope.macro_use.intro]
334
+
The *`macro_use`[attribute][attributes]* has two purposes: it may be used on modules to extend the scope of macros defined within them, and it may be used on [`extern crate`][items.extern-crate] to import macros from another crate into the [`macro_use` prelude].
336
335
337
-
```rust
338
-
#[macro_use]
339
-
modinner {
340
-
macro_rules!m {
341
-
() => {};
342
-
}
343
-
}
336
+
> [!EXAMPLE]
337
+
> ```rust
338
+
> #[macro_use]
339
+
> modinner {
340
+
> macro_rules!m {
341
+
> () => {};
342
+
> }
343
+
> }
344
+
> m!();
345
+
> ```
346
+
>
347
+
> ```rust,ignore
348
+
> #[macro_use]
349
+
> externcrate log;
350
+
> ```
344
351
345
-
m!();
346
-
```
352
+
r[macro.decl.scope.macro_use.syntax]
353
+
Whenusedonmodules, the `macro_use` attributeusesthe [MetaWord] syntax.
354
+
355
+
Whenusedon `externcrate`, it uses the [MetaWord] and [MetaListIdents] syntaxes. For more on how these syntaxes may be used, see [macro.decl.scope.macro_use.prelude].
356
+
357
+
r[macro.decl.scope.macro_use.allowed-positions]
358
+
The `macro_use` attribute may be applied to modules or `externcrate`.
359
+
360
+
> [!NOTE]
361
+
> `rustc` ignores usein other positions but lints against it. This may become an error in the future.
362
+
363
+
r[macro.decl.scope.macro_use.extern-crate-self]
364
+
The `macro_use` attribute may not be used on [`externcrate self`].
365
+
366
+
r[macro.decl.scope.macro_use.duplicates]
367
+
The `macro_use` attribute may be used any number of times on a form.
368
+
369
+
Multiple instances of `macro_use` in the [MetaListIdents] syntax may be specified. The union of all specified macros will be imported.
370
+
371
+
> [!NOTE]
372
+
> On modules, `rustc` lints against any [MetaWord] `macro_use` attributes following the first.
373
+
>
374
+
> On `externcrate`, `rustc` lints against any `macro_use` attributes that have no effect due to not importing any macros not already imported by another `macro_use` attribute. If two or more [MetaListIdents] `macro_use` attributes import the same macro, the first is linted against. If any [MetaWord] `macro_use` attributes are present, all [MetaListIdents] `macro_use` attributes are linted against. If two or more [MetaWord] `macro_use` attributes are present, the ones following the first are linted against.
375
+
376
+
r[macro.decl.scope.macro_use.mod-decl]
377
+
When `macro_use` is used on a module, the module's macro scope extends beyond the module's lexical scope.
378
+
379
+
> [!EXAMPLE]
380
+
> ```rust
381
+
> #[macro_use]
382
+
> mod inner {
383
+
> macro_rules! m {
384
+
> () => {};
385
+
> }
386
+
> }
387
+
> m!(); // OK
388
+
> ```
347
389
348
390
r[macro.decl.scope.macro_use.prelude]
349
-
Second, it can be used to import macros from another crate, by attaching it to
350
-
an `extern crate` declaration appearing in the crate's root module. Macros
351
-
imported this way are imported into the [`macro_use` prelude], not textually,
352
-
which means that they can be shadowed by any other name. While macros imported
353
-
by `#[macro_use]` can be used before the import statement, in case of a
354
-
conflict, the last macro imported wins. Optionally, a list of macros to import
355
-
can be specified using the [MetaListIdents] syntax; this is not supported
356
-
when `#[macro_use]` is applied to a module.
391
+
Specifying `macro_use` onan `externcrate` declaration in the crate root imports exported macros from that crate.
357
392
358
-
<!-- ignore: requires external crates -->
359
-
```rust,ignore
360
-
#[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
361
-
extern crate lazy_static;
393
+
Macros imported this way are imported into the [`macro_use` prelude], not textually, which means that they can be shadowed by any other name. While macros imported by `macro_use` can be used before the import statement, in case of a conflict, the last macro imported wins.
362
394
363
-
lazy_static!{}
364
-
// self::lazy_static!{} // Error: lazy_static is not defined in `self`
365
-
```
395
+
When using the [MetaWord] syntax, all exported macros are imported. When using the [MetaListIdents] syntax, only the specified macros are imported.
396
+
397
+
> [!EXAMPLE]
398
+
> <!-- ignore: requires external crates -->
399
+
> ```rust,ignore
400
+
> #[macro_use(lazy_static)] // Or `#[macro_use]` to import all macros.
401
+
> externcrate lazy_static;
402
+
>
403
+
> lazy_static!{}
404
+
> // self::lazy_static!{} // ERROR: lazy_static is not defined in `self`.
405
+
> ```
366
406
367
407
r[macro.decl.scope.macro_use.export]
368
-
Macros to be imported with `macro_use` must be exported with
0 commit comments