---
title: "Heap in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/heap/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Heap in Rust

Source: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/heap/

The [heap sample](https://developer.repebble.com/pebbleos/docs/samples/kernel/heap.md) written in [Rust](https://developer.repebble.com/pebbleos/docs/development/build_system/rust.md), on the `pebbleos` crate.

`Heap<N>` sets itself up and takes a lock on first use; each `Block` it returns goes back to it when dropped, so freeing every other block is emptying every other slot of an array.

Run:

`pbl samples samples/rust/kernel/heap`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use pebbleos::heap::{Block, Heap};use pebbleos::{Errno, Result, println};const BLOCK\_SIZE: usize = 100;// A heap of its own bounds what one user can take, here a 2 KiB cache.static HEAP: Heap\<2048\> = Heap::new();fn log\_usage(when: &str) {let stats = HEAP.stats();println!("heap: {when}: {} used, {} free, largest free block {}",stats.used, stats.free, stats.largest\_free);}#[pebbleos::main]fn main() -\> Result\<()\> {let mut blocks: [Option\<Block\>; 32] = [const { None }; 32];log\_usage("empty");// Allocation fails with None, which the caller handles.let name = HEAP.alloc\_copy(b"a string on the heap").ok\_or(Errno::ENOMEM)?;println!("heap: block is in the heap: {}, allocated: {}",yes\_no(HEAP.contains(name.as\_ptr())),yes\_no(HEAP.is\_allocated(name.as\_ptr())));// A block goes back to its heap when dropped.drop(name);let mut num\_blocks = 0;for slot in &mut blocks {\*slot = HEAP.alloc(BLOCK\_SIZE);if slot.is\_none() {break;}num\_blocks += 1;}println!("heap: full after {num\_blocks} blocks of {BLOCK\_SIZE} bytes");log\_usage("full");// Every other block freed: plenty of memory in total, but in pieces.for slot in blocks.iter\_mut().step\_by(2) {\*slot = None;}log\_usage("every other block freed");let big = HEAP.alloc(2 \* BLOCK\_SIZE);println!("heap: {} bytes from a fragmented heap: {}",2 \* BLOCK\_SIZE,if big.is\_none() {"refused"} else {"allocated"});// Freed neighbors merge, so the pieces become one block again.drop(blocks);log\_usage("all freed");println!("heap: least memory it ever had left: {} of {} bytes",HEAP.min\_headroom(),HEAP.size());println!("Sample done");Ok(())}fn yes\_no(b: bool) -\> &'static str {if b { "yes" } else { "no" }}
```

## Output

```
heap: empty: 0 used, \d+ free, largest free block \d+
heap: block is in the heap: yes, allocated: yes
heap: full after \d+ blocks of 100 bytes
heap: full: \d+ used, \d+ free, largest free block \d+
heap: every other block freed: \d+ used, \d+ free, largest free block \d+
heap: 200 bytes from a fragmented heap: refused
heap: all freed: 0 used, \d+ free, largest free block \d+
heap: least memory it ever had left: \d+ of \d+ bytes
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/rust/kernel/heap.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/heap.md).
