Intro to Embedded Rust Part 8: Lifetimes and Lifetime Annotations | DigiKey
In this video, dive into lifetimes and how to use lifetime annotations to help Rust’s compiler and borrow checker. Lifetimes are Rust's way of ensuring that references never outlive the data they point to, preventing dangerous bugs like use-after-free, dangling pointers, and memory corruption that plague C and C++ programs. The written guide for this episode can be found here: https://www.digikey.com/en/maker/tutorials/2026/intro-to-embedded-rust-part-8-lifetimes-and-lifetime-annotations The GitHub repository containing the Docker image and example code for this series can be found here: https://github.com/ShawnHymel/introduction-to-embedded-rust While the Rust compiler can automatically infer lifetimes in many common cases through "lifetime elision," understanding when and how to write explicit lifetime annotations is essential for working with complex code, especially in embedded Rust where structs often hold references to hardware resources, peripheral configurations, and DMA buffers. This tutorial provides clear, practical examples using stack-based sensor data structures, making lifetime concepts accessible without requiring heap allocation or the standard library. In the video, we walk through five examples of lifetime annotations. We start by explaining what lifetimes are: measures of how long references remain valid, tied to the scope in which data exists. You'll see how the borrow checker prevents dangling pointers by analyzing scopes and rejecting code where references outlive their data. We explore lifetime elision rules that allow the compiler to automatically infer lifetimes in simple cases, like functions with a single reference parameter or methods with &self. We then demonstrate situations requiring explicit annotations: functions with multiple reference parameters where the compiler can't determine which input the output is tied to, showing how lifetime parameters like <'a> document the relationships between input and output references for the compiler's verification. We also look at annotations in struct definitions, where any struct holding references must declare lifetime parameters to ensure instances can't outlive the data they borrow. We demonstrate how to implement methods for structs with lifetime parameters, requiring you to declare lifetimes in both the struct definition and the impl block. We’ll see how method return values can be explicitly tied to struct field lifetimes rather than relying on elision, making the code's intent clear and enabling the compiler to verify safety. Finally, we cover the special 'static lifetime for data that lives for the entire program duration, commonly used in embedded systems for configuration data, lookup tables, and constants stored in flash memory. We explain the difference between static and const declarations and when to use 'static annotations for global data.
Part List
| Imagem | Número de peça do fabricante | Descrição | Available Quantity | Preço | Ver detalhes | |
|---|---|---|---|---|---|---|
![]() | ![]() | SC1631 | RASPBERRY PI PICO 2 RP2350 | 26210 - Immediate | $5.00 | Ver detalhes |
![]() | ![]() | SC1632 | RASPBERRY PI PICO 2 H RP2350 | 1693 - Immediate | $6.00 | Ver detalhes |
![]() | ![]() | SC1633 | RASPBERRY PI PICO 2 W RP2350 | 6111 - Immediate | $7.00 | Ver detalhes |
![]() | ![]() | SC1634 | RASPBERRY PI PICO 2 WH RP2350 | 2388 - Immediate | $8.00 | Ver detalhes |
![]() | ![]() | 13314 | TMP102 DIGITAL TEMP SENSOR BOARD | 254 - Immediate | $5.95 | Ver detalhes |
![]() | ![]() | LTL-4224 | LED RED CLEAR T-1 3/4 T/H | 73475 - Immediate | $0.14 | Ver detalhes |
![]() | ![]() | CF14JT220R | RES 220 OHM 5% 1/4W AXIAL | 93369 - Immediate 30000 - Factory Stock | $0.10 | Ver detalhes |
![]() | ![]() | 1825910-6 | SWITCH TACTILE SPST-NO 0.05A 24V | 45941 - Immediate | $0.13 | Ver detalhes |
![]() | ![]() | FIT0096 | BREADBRD TERM STRIP 3.20X2.00" | 3713 - Immediate | $2.90 | Ver detalhes |
![]() | ![]() | 1957 | JUMPER WIRE M TO M 6" 28AWG | 3907 - Immediate | $1.95 | Ver detalhes |
![]() | ![]() | DH-20M50055 | USB AM TO USB MICRO, USB 2.0 - 1 | 9354 - Immediate | $1.55 | Ver detalhes |
















