Rust for Embedded Systems

no_std, the embedded HAL ecosystem, and a blinky program for an STM32 board.

Rust runs on microcontrollers without an operating system. Its zero-cost abstractions and compile-time checks are especially valuable there: the type system can encode which pins are outputs, and the borrow checker prevents two parts of the firmware from using the same peripheral at once.

The ecosystem

static mut LED_ON: bool = false; // shared with an interrupt

Use a critical-section Mutex, an atomic (AtomicBool), or a framework like RTIC

Accessing a static mut is unsafe because interrupts can race with the main loop. Atomics and critical sections make sharing safe.

What does #![no_std] mean?

The crate does not link the standard library, only core (and optionally alloc), because there is no operating system providing files, threads or a heap.

How does the HAL prevent writing to an input pin?

Each pin mode is a different type; output methods only exist on the output type, so misuse is a compile error.