@@ -14,9 +14,34 @@ can be used to/for:
14
14
* mark functions that will be part of a benchmark
15
15
* [ attribute like macros] [ macros ]
16
16
17
- When attributes apply to a whole crate, their syntax is ` #![crate_attribute] ` ,
18
- and when they apply to a module or item, the syntax is ` #[item_attribute] `
19
- (notice the missing bang ` ! ` ).
17
+ Attributes look like ` #[outer_attribute] ` or ` #![inner_attribute] ` ,
18
+ with the difference between them being where they apply.
19
+
20
+ - ` #[outer_attribute] ` applies to the [ item] [ item ] immediately
21
+ following it. Some examples of items are: a function, a module
22
+ declaration, a constant, a structure, an enum. Here is an example
23
+ where attribute ` #[derive(Debug)] ` applies to the struct
24
+ ` Rectangle ` :
25
+ ``` rust
26
+ #[derive(Debug )]
27
+ struct Rectangle {
28
+ width : u32 ,
29
+ height : u32 ,
30
+ }
31
+ ```
32
+
33
+ - ` #![inner_attribute] ` applies to the enclosing [ item] [ item ] (typically a
34
+ module or a crate). In other words, this attribute is intepreted as
35
+ applying to the entire scope in which it's place. Here is an example
36
+ where ` #![allow(unusude_variables)] ` applies to the whole crate (if
37
+ placed in ` main.rs ` ):
38
+ ``` rust
39
+ #![allow(unused_variables)]
40
+
41
+ fn main () {
42
+ let x = 3 ; // This would normally warn about an unused variable.
43
+ }
44
+ ```
20
45
21
46
Attributes can take arguments with different syntaxes:
22
47
@@ -36,5 +61,6 @@ Attributes can have multiple values and can be separated over multiple lines, to
36
61
37
62
[ cfg ] : attribute/cfg.md
38
63
[ crate ] : attribute/crate.md
64
+ [ item ] : https://doc.rust-lang.org/stable/reference/items.html
39
65
[ lint ] : https://en.wikipedia.org/wiki/Lint_%28software%29
40
66
[ macros ] : https://doc.rust-lang.org/book/ch19-06-macros.html#attribute-like-macros
0 commit comments