Skip to content

Commit 2c0ea0b

Browse files
committed
Move used example into an example block
1 parent c61f0e2 commit 2c0ea0b

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

src/abi.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,48 @@ r[abi.used]
1515
r[abi.used.intro]
1616
The *`used` [attribute]* forces a [`static` item][items.static] to be kept in the output object file (.o, .rlib, etc. excluding final binaries) even if the static is never used, or referenced, by an other item in the crate. However, the linker is still free to remove such an item.
1717

18-
Below is an example that shows under what conditions the compiler keeps a `static` item in the output object file.
19-
20-
``` rust
21-
// foo.rs
22-
23-
// This is kept because of `#[used]`:
24-
#[used]
25-
static FOO: u32 = 0;
26-
27-
// This is removable because it is unused:
28-
#[allow(dead_code)]
29-
static BAR: u32 = 0;
30-
31-
// This is kept because it is publicly reachable:
32-
pub static BAZ: u32 = 0;
33-
34-
// This is kept because it is referenced by a public, reachable function:
35-
static QUUX: u32 = 0;
36-
37-
pub fn quux() -> &'static u32 {
38-
&QUUX
39-
}
40-
41-
// This is removable because it is referenced by a private, unused (dead) function:
42-
static CORGE: u32 = 0;
43-
44-
#[allow(dead_code)]
45-
fn corge() -> &'static u32 {
46-
&CORGE
47-
}
48-
```
49-
50-
``` console
51-
$ rustc -O --emit=obj --crate-type=rlib foo.rs
52-
53-
$ nm -C foo.o
54-
0000000000000000 R foo::BAZ
55-
0000000000000000 r foo::FOO
56-
0000000000000000 R foo::QUUX
57-
0000000000000000 T foo::quux
58-
```
18+
> [!EXAMPLE]
19+
> This example that shows under what conditions the compiler keeps a `static` item in the output object file.
20+
>
21+
> ```rust
22+
> // foo.rs
23+
>
24+
> // This is kept because of `#[used]`:
25+
> #[used]
26+
> static FOO: u32 = 0;
27+
>
28+
> // This is removable because it is unused:
29+
> #[allow(dead_code)]
30+
> static BAR: u32 = 0;
31+
>
32+
> // This is kept because it is publicly reachable:
33+
> pub static BAZ: u32 = 0;
34+
>
35+
> // This is kept because it is referenced by a public, reachable function:
36+
> static QUUX: u32 = 0;
37+
>
38+
> pub fn quux() -> &'static u32 {
39+
> &QUUX
40+
> }
41+
>
42+
> // This is removable because it is referenced by a private, unused (dead) function:
43+
> static CORGE: u32 = 0;
44+
>
45+
> #[allow(dead_code)]
46+
> fn corge() -> &'static u32 {
47+
> &CORGE
48+
> }
49+
> ```
50+
>
51+
> ```console
52+
> $ rustc -O --emit=obj --crate-type=rlib foo.rs
53+
>
54+
> $ nm -C foo.o
55+
> 0000000000000000 R foo::BAZ
56+
> 0000000000000000 r foo::FOO
57+
> 0000000000000000 R foo::QUUX
58+
> 0000000000000000 T foo::quux
59+
> ```
5960
6061
r[abi.no_mangle]
6162
## The `no_mangle` attribute

0 commit comments

Comments
 (0)