VideoLibrary

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.

3/6/2026 4:51:35 PM

Part List

ImageManufacturer Part NumberDescriptionAvailable QuantityPriceView Details
RASPBERRY PI PICO 2 RP2350SC1631RASPBERRY PI PICO 2 RP235026210 - Immediate$19.75View Details
RASPBERRY PI PICO 2 H RP2350SC1632RASPBERRY PI PICO 2 H RP23501693 - Immediate$23.70View Details
RASPBERRY PI PICO 2 W RP2350SC1633RASPBERRY PI PICO 2 W RP23506111 - Immediate$27.65View Details
RASPBERRY PI PICO 2 WH RP2350SC1634RASPBERRY PI PICO 2 WH RP23502388 - Immediate$31.60View Details
TMP102 DIGITAL TEMP SENSOR BOARD13314TMP102 DIGITAL TEMP SENSOR BOARD254 - Immediate$23.50View Details
LED RED CLEAR T-1 3/4 T/HLTL-4224LED RED CLEAR T-1 3/4 T/H73475 - Immediate$0.55View Details
RES 220 OHM 5% 1/4W AXIALCF14JT220RRES 220 OHM 5% 1/4W AXIAL93369 - Immediate
30000 - Factory Stock
$0.40View Details
SWITCH TACTILE SPST-NO 0.05A 24V1825910-6SWITCH TACTILE SPST-NO 0.05A 24V45941 - Immediate$0.51View Details
BREADBRD TERM STRIP 3.20X2.00"FIT0096BREADBRD TERM STRIP 3.20X2.00"3713 - Immediate$11.46View Details
JUMPER WIRE M TO M 6" 28AWG1957JUMPER WIRE M TO M 6" 28AWG3907 - Immediate$7.70View Details
USB AM TO USB MICRO, USB 2.0 - 1DH-20M50055USB AM TO USB MICRO, USB 2.0 - 19354 - Immediate$6.12View Details