Rust Tutorials for Raspberry Pi¶
Learn stable Rust from values and ownership to traits, lifetimes, concurrency and a tested Raspberry Pi Linux application. The language course is complete, with 29 lessons organised into four parts. You can learn the language on another supported computer; GPIO equipment and C++ knowledge are not prerequisites.
Where to start¶
- New to Rust: start with installing Rust and running Cargo, then follow the parts below.
- Already using Rust: choose a topic in the roadmap, or use the Reference reading map to find a specific rule.
- Ready to combine the concepts: build the read-only system-status project and test partial failures before reading live Linux data.
Runnable examples on GitHub¶
The Rust Learning examples repository contains the existing code from all 41 lessons, including the advanced comparison/design series. Each lesson links to its own files, commands and expected output; the repository links back here. Article source is reused, not rewritten.
Use Cargo rather than CMake. Most lessons share a category workspace; the macro lesson preserves its existing two-package workspace. Intentional compile/test failures are isolated and checked separately. The CLI and system-status projects include fixtures so you can run them without sensors or live device access. The existing C++ comparison programs are included alongside their Rust lessons.
Language course¶
All articles linked in Parts 1–4 are published. Each part has a checkpoint; the final lesson reviews the course. Article titles are unnumbered, as in the C++ course. Follow the list order or the previous/next links within each article.
Part 1: Values, Ownership and Data¶
Build small programs, understand who owns each value, and distinguish missing data from invalid input.
- Install Rust and Cargo
- Variables, functions and control flow
- Types, operators and expressions
- Ownership, moves, Copy and Clone
- References, borrowing and slices
- Structs, methods and associated functions
- Enums, match and patterns
- Strings, vectors and hash maps
- Option, Result and error handling
Checkpoint: explain and extend the input report. Zero is a measured value, not absence.
Part 2: Abstraction and APIs¶
Separate project boundaries from type relationships, then test the contract a caller sees.
- Crates, modules, paths and visibility
- Generics, bounds and const generics
- Traits, implementations and associated types
- Lifetimes and borrowed APIs
- Trait objects and dispatch
- Closures and function pointers
- Iterators and lazy adapters
- Tests, documentation and Cargo
Checkpoint: defend and extend the documented public API. Use unit, integration and documentation tests without confusing tests with a proof of correctness.
Part 3: Resources and Advanced Rust¶
Study resource cleanup and concurrency before distinguishing async suspension, pinning and unsafe implementation obligations.
- Box, Deref and Drop
- Rc, Weak, Cell and RefCell
- Threads, Send, Sync, Arc and locks
- Channels, atomics and shutdown
- async, await and Future
- Pin, Unpin and async boundaries
- Declarative and procedural macros
- Attributes, cfg, features and editions
- Unsafe Rust, raw pointers and FFI
Checkpoint: defend ownership and worker shutdown. Name the owner, blocking operations and completion policy; adding more advanced features is not itself a better design.
Part 4: Integration and Reference¶
Apply the language to a small native Linux tool, then learn how to investigate the governing rules.
Checkpoint: test the integrated system snapshot, including fixtures, read/write failures and strict exit policy. Finish with the course review.
Advanced design series after the language course¶
This separate twelve-lesson series follows Parts 1–4 and is not needed to complete the language course. Part 5 compares Rust and C++ contracts; Part 6 revisits design patterns by requirements and change boundaries. Each part ends with comparison exercises.
Part 5: Rust and C++¶
- Values, ownership and RAII: compare matched cleanup requirements, early returns and the limits of destructor-based protocols.
- Borrowing, const and lifetimes: separate an access restriction from exclusive borrowing, interior mutability and independently retained data.
- Traits, generics and dispatch: compare constrained concrete calls with borrowed or owned heterogeneous interfaces.
- Errors, exceptions and contracts: compare expected input failures, scope cleanup and panic boundaries without treating them as one mechanism.
- Rust moves vs C++ move semantics: separate ownership transfer, moved-from states, reusable slots and closure call contracts.
- Address stability and Pin: distinguish moving an owner from relocating its pointee, and explain why stable addresses do not extend lifetimes.
Checkpoint: defend the six comparison contracts. Similar names do not establish identical mechanisms.
Part 6: Design in Rust¶
Revisit design patterns by the problem each solves. Evaluate change requirements, invariants and failure behaviour rather than assuming a pattern or language is universally better.
- Strategy: functions, traits and dispatch: choose callables, named policies or runtime interfaces by state and ownership requirements.
- State: enums and typestate: compare runtime rejection with state-specific operations, consuming transitions and retry ownership.
- Factory and Builder: validated construction: share validation across construction paths and distinguish missing choices from zero.
- Visitor: enums and open behaviour: compare adding operations over known variants with accepting new implementations of a behaviour.
- Observer: events and shutdown: define callback ownership, removal timing, delivery order and explicit termination.
- Ownership, IDs and ECS design: distinguish entity lifetime from persistent identity, and test reuse, cleanup and retirement before choosing a layout.
Checkpoint: defend the six design boundaries. Compare the requirement and change cost, not a pattern name or an unmeasured speed claim.
Requirements and scope¶
You need basic terminal navigation and a supported Rust toolchain. Examples use stable Rust and edition 2024; verification notes record the actual compiler and environment. Start with fixtures, so the early lessons do not depend on a board or sensor.
The native Pi workflow uses 64-bit user space. Check the installed OS architecture rather than inferring it from the board model. Older boards and 32-bit installations need a toolchain/target compatibility check.
This is practical language fluency, not an exhaustive specification or unsafe-code manual. Language rules, standard-library APIs, Cargo and third-party runtimes are distinguished. Rust does not automatically fix permissions, wiring, deadlocks, logical errors or every external-interface obligation. No hardware speed, power or safety benchmark is claimed.
For a hardware follow-on, read the Raspberry Pi model and try an LED with Rust and RPPAL. The same project gives you an optional ARM64 cross-compilation exercise. Raspberry Pi Linux applications and Pico firmware use different targets.
References and related courses¶
- The Rust Programming Language: guided explanations alongside the lessons.
- The Rust Reference: stable language rules, separate from library and tool documentation.
- Official installation instructions: toolchain installation.
- Essential Linux commands: terminal preparation.
- Modern C++: the separate C++ course.
- ECS design: a separate design comparison, not a Rust-course prerequisite.