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
+47-19Lines changed: 47 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -329,31 +329,59 @@ fn foo() {
329
329
r[macro.decl.scope.macro_use]
330
330
### The `macro_use` attribute
331
331
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 module's macro scope not end when the module is closed, by applying it to a module:
332
+
r[macro.decl.scope.macro_use.intro]
333
+
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.
334
334
335
-
```rust
336
-
#[macro_use]
337
-
modinner {
338
-
macro_rules!m {
339
-
() => {};
340
-
}
341
-
}
335
+
r[macro.decl.scope.macro_use.syntax]
336
+
When used on a module, the `macro_use` attribute uses the [MetaWord] syntax and thus does not take any inputs.
342
337
343
-
m!();
344
-
```
338
+
When used on an `extern crate`, it uses either the [MetaWord] or [MetaListIdents] syntax.
339
+
340
+
r[macro.decl.scope.macro_use.allowed-positions]
341
+
The `macro_use` attribute may be applied to modules or `extern crate`.
342
+
343
+
> [!NOTE]
344
+
> `rustc` currently warns in other positions, but this may be rejected in the future.
345
+
346
+
r[macro.decl.scope.macro_use.duplicates]
347
+
Duplicate instances of `macro_use` that are in the [MetaWord] syntax have no effect if there is already a `macro_use` with the [MetaWord] syntax.
348
+
349
+
Multiple instances of `macro_use` that are in the [MetaListIdents] syntax may be specified. The union of all specified macros to import will be imported.
350
+
351
+
> [!NOTE]
352
+
> `rustc` warns about duplicate [MetaWord]`macro_use` attributes.
353
+
354
+
r[macro.decl.scope.macro_use.mod-decl]
355
+
When `macro_use` is used on a module, it causes the module's macro scope to not end when the module is closed.
356
+
357
+
> [!EXAMPLE]
358
+
> ```rust
359
+
> #[macro_use]
360
+
> modinner {
361
+
> macro_rules!m {
362
+
> () => {};
363
+
> }
364
+
> }
365
+
>
366
+
> m!();
367
+
> ```
345
368
346
369
r[macro.decl.scope.macro_use.prelude]
347
-
Second, it can be used to import macros from another crate, by attaching it to an `extern crate` declaration appearing in the crate's root module. 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. Optionally, a list of macros to import can be specified using the [MetaListIdents] syntax; this is not supported when `#[macro_use]` is applied to a module.
370
+
When `macro_use` isusedonan `externcrate` declaration in the crate root, it imports exported macros from that crate.
348
371
349
-
<!-- ignore: requires external crates -->
350
-
```rust,ignore
351
-
#[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
352
-
extern crate lazy_static;
372
+
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.
353
373
354
-
lazy_static!{}
355
-
// self::lazy_static!{} // Error: lazy_static is not defined in `self`
356
-
```
374
+
When using the [MetaWord] syntax, all exported macros are imported. When using the [MetaListIdents] syntax, only the specified macros are imported.
375
+
376
+
> [!EXAMPLE]
377
+
> <!-- ignore: requires external crates -->
378
+
> ```rust,ignore
379
+
> #[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
380
+
> externcrate lazy_static;
381
+
>
382
+
> lazy_static!{}
383
+
> // self::lazy_static!{} // Error: lazy_static is not defined in `self`
0 commit comments