@@ -6,8 +6,43 @@ This section documents features that define some aspects of the Rust runtime.
6
6
r[ runtime.global_allocator]
7
7
## The ` global_allocator ` attribute
8
8
9
- The * ` global_allocator ` attribute* is used on a [ static item] implementing the
10
- [ ` GlobalAlloc ` ] trait to set the global allocator.
9
+ r[ runtime.global_allocator.intro]
10
+ The * ` global_allocator ` [ attribute] [ attributes ] * selects a [ memory allocator] [ std::alloc ] .
11
+
12
+ > [ !EXAMPLE]
13
+ > ``` rust
14
+ > use core :: alloc :: {GlobalAlloc , Layout };
15
+ > use std :: alloc :: System ;
16
+ >
17
+ > struct MyAllocator ;
18
+ >
19
+ > unsafe impl GlobalAlloc for MyAllocator {
20
+ > unsafe fn alloc (& self , layout : Layout ) -> * mut u8 {
21
+ > unsafe { System . alloc (layout ) }
22
+ > }
23
+ > unsafe fn dealloc (& self , ptr : * mut u8 , layout : Layout ) {
24
+ > unsafe { System . dealloc (ptr , layout ) }
25
+ > }
26
+ > }
27
+ >
28
+ > #[global_allocator]
29
+ > static GLOBAL : MyAllocator = MyAllocator ;
30
+ > ```
31
+
32
+ r [runtime . global_allocator. syntax]
33
+ The `global_allocator ` attribute uses the [MetaWord ] syntax .
34
+
35
+ r [runtime . global_allocator. allowed- positions ]
36
+ The `global_allocator ` attribute may only be applied to a [static item ] whose type implements the [`GlobalAlloc `] trait .
37
+
38
+ r [runtime . global_allocator. duplicates]
39
+ The `global_allocator ` attribute may only be used once on an item .
40
+
41
+ r [runtime . global_allocator. single]
42
+ The `global_allocator ` attribute may only be used once in the crate graph .
43
+
44
+ r [runtime . global_allocator. stdlib]
45
+ The `global_allocator ` attribute is exported from the [standard library prelude ][core :: prelude :: v1 ].
11
46
12
47
r [runtime . windows_subsystem]
13
48
## The `windows_subsystem ` attribute
0 commit comments