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

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

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

`DelayedWork<T>` holds the data its function gets, here the counts.

Run:

`pbl samples samples/rust/kernel/delayed_work`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use core::sync::atomic::{AtomicU32, Ordering};use core::time::Duration;use pebbleos::println;use pebbleos::thread;use pebbleos::work::{DelayedWork, WorkQueue};const DEBOUNCE: Duration = Duration::from\_millis(200);const HEARTBEAT: Duration = Duration::from\_millis(20);static DEBOUNCE\_WORK: DelayedWork\<AtomicU32\> = DelayedWork::new(button\_settled, AtomicU32::new(0));static HEARTBEAT\_WORK: DelayedWork\<AtomicU32\> = DelayedWork::new(heartbeat, AtomicU32::new(0));fn button\_settled(presses: &'static DelayedWork\<AtomicU32\>) {let presses = presses.fetch\_add(1, Ordering::Relaxed) + 1;println!("button: settled, press {presses}");}// Unlike a timer function this runs on a thread, and may block or take locks.fn heartbeat(beats: &'static DelayedWork\<AtomicU32\>) {beats.fetch\_add(1, Ordering::Relaxed);// Counts from the last expiry, not from now, so the period does not drift.beats.schedule\_next(HEARTBEAT);}// What a button interrupt would do on every edge, bounces included.fn button\_edge() {DEBOUNCE\_WORK.reschedule(WorkQueue::system(), DEBOUNCE);}#[pebbleos::main]fn main() {let system = WorkQueue::system();println!("main: five edges 5 ms apart, each pushing the work back");for \_ in 0..5 {button\_edge();thread::sleep(Duration::from\_millis(5));}thread::sleep(2 \* DEBOUNCE);// schedule() keeps a delay that is running, where reschedule() replaces// it.DEBOUNCE\_WORK.schedule(system, DEBOUNCE);let kept = !DEBOUNCE\_WORK.schedule(system, Duration::from\_secs(10));println!("main: scheduled twice, first delay kept: {}", yes\_no(kept));thread::sleep(2 \* DEBOUNCE);HEARTBEAT\_WORK.schedule(system, HEARTBEAT);thread::sleep(5 \* HEARTBEAT + HEARTBEAT / 2);// Stops it for good, even though its function schedules it again.HEARTBEAT\_WORK.cancel\_sync();println!("main: heartbeat stopped after {} beats, pending: {}",HEARTBEAT\_WORK.load(Ordering::Relaxed),yes\_no(HEARTBEAT\_WORK.is\_pending()));println!("Sample done");}fn yes\_no(b: bool) -\> &'static str {if b { "yes" } else { "no" }}
```

## Output

```
main: five edges 5 ms apart, each pushing the work back
button: settled, press 1
main: scheduled twice, first delay kept: yes
button: settled, press 2
main: heartbeat stopped after \d beats, pending: no
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/delayed\_work.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/delayed_work.md).
