Skip to content

Commit 00a2761

Browse files
committed
Fix and extend the explanation of outer vs inner attributes.
1 parent c954202 commit 00a2761

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/attribute.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,34 @@ can be used to/for:
1414
* mark functions that will be part of a benchmark
1515
* [attribute like macros][macros]
1616

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+
```
2045

2146
Attributes can take arguments with different syntaxes:
2247

@@ -36,5 +61,6 @@ Attributes can have multiple values and can be separated over multiple lines, to
3661

3762
[cfg]: attribute/cfg.md
3863
[crate]: attribute/crate.md
64+
[item]: https://doc.rust-lang.org/stable/reference/items.html
3965
[lint]: https://en.wikipedia.org/wiki/Lint_%28software%29
4066
[macros]: https://doc.rust-lang.org/book/ch19-06-macros.html#attribute-like-macros

0 commit comments

Comments
 (0)