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
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
+
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].
335
+
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
+
> ```
335
351
336
352
r[macro.decl.scope.macro_use.syntax]
337
-
When used on a module, the `macro_use` attribute uses the [MetaWord] syntax.
353
+
Whenusedonmodules, the `macro_use` attributeusesthe [MetaWord] syntax.
338
354
339
-
When used on an `extern crate`, it uses either the [MetaWord]or[MetaListIdents]syntax (described in [macro.decl.scope.macro_use.prelude]).
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].
340
356
341
357
r[macro.decl.scope.macro_use.allowed-positions]
342
358
The `macro_use` attribute may be applied to modules or `externcrate`.
@@ -350,13 +366,15 @@ The `macro_use` attribute may not be used on [`extern crate self`].
350
366
r[macro.decl.scope.macro_use.duplicates]
351
367
The `macro_use` attribute may be used any number of times on a form.
352
368
353
-
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.
369
+
Multiple instances of `macro_use` in the [MetaListIdents] syntax may be specified. The union of all specified macros will be imported.
354
370
355
371
> [!NOTE]
356
-
> `rustc` warns about duplicate [MetaWord]`macro_use` attributes.
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.
357
375
358
376
r[macro.decl.scope.macro_use.mod-decl]
359
-
When `macro_use` is used on a module, it causes the module's macro scope to not end when the module is closed.
377
+
When `macro_use` is used on a module, the module's macro scope extends beyond the module's lexical scope.
360
378
361
379
> [!EXAMPLE]
362
380
> ```rust
@@ -366,8 +384,7 @@ When `macro_use` is used on a module, it causes the module's macro scope to not
366
384
> () => {};
367
385
> }
368
386
> }
369
-
>
370
-
> m!();
387
+
> m!(); // OK
371
388
> ```
372
389
373
390
r[macro.decl.scope.macro_use.prelude]
@@ -380,11 +397,11 @@ When using the [MetaWord] syntax, all exported macros are imported. When using t
380
397
> [!EXAMPLE]
381
398
> <!-- ignore: requires external crates -->
382
399
> ```rust,ignore
383
-
> #[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
400
+
> #[macro_use(lazy_static)] // Or `#[macro_use]` to import all macros.
384
401
> externcrate lazy_static;
385
402
>
386
403
> lazy_static!{}
387
-
> // self::lazy_static!{} // Error: lazy_static is not defined in `self`
404
+
> // self::lazy_static!{} // ERROR: lazy_static is not defined in `self`.
0 commit comments