Skip to content

Commit 93706cb

Browse files
committed
Add a code example to README.md
1 parent 79d220b commit 93706cb

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,37 @@ This project is developed and maintained by the [Cortex-M team][team].
1212

1313
## Example
1414

15-
For a usage example, see `examples/global_alloc.rs`.
15+
Starting with Rust 1.68, this crate can be used as a global allocator:
16+
17+
```
18+
#![no_std]
19+
#![no_main]
20+
21+
extern crate alloc;
22+
23+
use cortex_m_rt::entry;
24+
use embedded_alloc::Heap;
25+
26+
#[global_allocator]
27+
static HEAP: Heap = Heap::empty();
28+
29+
#[entry]
30+
fn main() -> ! {
31+
// Initialize the allocator BEFORE you use it
32+
{
33+
use core::mem::MaybeUninit;
34+
const HEAP_SIZE: usize = 1024;
35+
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
36+
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
37+
}
38+
39+
// now the allocator is ready types like Box, Vec can be used.
40+
41+
loop { /* .. */ }
42+
}
43+
```
44+
45+
For a full usage example, see `examples/global_alloc.rs`.
1646

1747
## [Documentation](https://docs.rs/embedded-alloc)
1848

0 commit comments

Comments
 (0)