Introduction to Embedded Systems Development with Rust on Raspberry Pi¶
For decades, C and C++ have dominated embedded systems and hardware-level programming. However, modern systems demand both raw performance and absolute security. Rust is a system-level language that guarantees memory safety and thread safety at compile time, eliminating common bugs like null pointer dereferences, buffer overflows, and data races.
This guide shows you how to set up a Rust development environment for Raspberry Pi, configure cross-compilation, and write safe GPIO control code.
1. Why Choose Rust for Raspberry Pi?¶
- Memory Safety: The Rust compiler validates memory access patterns at compile-time (via Ownership and Borrowing rules), eliminating segmentation faults.
- Zero-Cost Abstractions: High-level abstractions (like iterators, pattern matching, and closures) compile down to machine instructions as fast as hand-written C.
- Concurrency Safety: Rust prevents data races (multiple threads trying to write to the same memory address concurrently) by validating data sharing at compile-time.
- Modern Tooling:
cargohandles dependency management, compiling, and testing seamlessly.
2. Setting Up the Rust Cross-Compiler¶
While you can compile Rust code directly on a Raspberry Pi, compiling on a more powerful host PC (Linux, macOS, or Windows) is significantly faster. We will configure cross-compilation to target Raspberry Pi's 64-bit architecture (aarch64-unknown-linux-gnu).
1. Install Rust on Your Host PC¶
Run the official Rust installer:
2. Add the Target Architecture¶
Add the toolchain target for 64-bit Raspberry Pi OS:
3. Install Cross-Compilation Linkers (Ubuntu/Debian Host)¶
4. Configure Cargo Linker¶
Create a global Cargo config file (or a local .cargo/config.toml in your project root) and specify the linker:
3. Creating a Rust Hardware Project¶
1. Initialize a Cargo Binary Project¶
2. Add the rppal Library Dependency¶
rppal (Raspberry Pi Peripheral Access Library) is the standard Rust crate providing safe access to GPIO, I2C, SPI, PWM, and UART.
Open Cargo.toml and add:
4. Writing GPIO Logic in Rust¶
Let's write a program that sets up a GPIO output pin (LED) and an input pin (button), turning on the LED only when the button is pressed.
Open src/main.rs and write:
Safety Features of rppal:¶
- Ownership validation: If you try to call
gpio.get(18)twice,rppalreturns an error, preventing two independent parts of your code from trying to configure the same physical hardware pin simultaneously. - Resource Cleanup: When variables
ledandbuttongo out of scope, Rust's drop checker automatically cleans up the pin configuration, acting as an automatic hardware RAII guard.
5. Compiling and Transferring Binaries¶
1. Compile the project¶
Build the binary targeting the Raspberry Pi:
2. Transfer the Binary to your Pi¶
3. Run on Raspberry Pi¶
SSH into your Raspberry Pi and execute the binary:
Related Guides¶
- Programming Basics — Digital input and output circuit setups.
- High-Speed GPIO Control — Detailed registry manipulation patterns.