rust-lifetimes

Apply Rust lifetime annotations to prevent dangling references in functions and structs.

Updated May 26, 2026
One-click install
npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill rust-lifetimes
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-lifetimes
Source: https://github.com/adelabdelgawad/rust-fullstack-agents/tree/main/plugins/rusty/skills/rust-lifetimes
Command: npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill rust-lifetimes

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Lifetimes are a core Rust concept that prevent dangling references by requiring explicit lifetime relationships between references, but they can be confusing and error-prone for developers. This Skill clarifies why lifetimes exist, when to annotate, and how to apply the three elision rules to write correct, safe code.

Core Features & Use Cases

  • Explain lifetimes & why they exist to prevent dangling references.
  • Clarify the three elision rules and when explicit annotations are required.
  • Guide common patterns such as structs with reference fields and functions returning references, with practical examples.

Quick Start

Experiment with a function that returns a reference and add explicit lifetime annotations to satisfy the borrow checker.

Frequently Asked Questions about rust-lifetimes

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why do Rust lifetimes exist and what problem do they solve?

Rust lifetimes exist to prevent dangling references by enforcing explicit relationships between references. They ensure references never outlive the data they point to, resolving memory-safety issues at compile time without runtime overhead.

How do I fix compile errors when a Rust function returns a reference?

To fix compile errors when a function returns a reference, add explicit lifetime annotations to the function signature. This tells the borrow checker how the return reference relates to input parameters, satisfying memory safety requirements.

When should I use explicit lifetime annotations instead of relying on elision rules?

Use explicit lifetime annotations when the three elision rules cannot infer the correct reference relationships, such as struct fields containing references or functions returning references with multiple input lifetimes.

How do I define structs with reference fields in Rust?

To define structs with reference fields, add explicit lifetime parameters to the struct definition and its implementation block. This guarantees that any references stored in the struct remain valid for the struct's lifetime.

When should I avoid using the 'static lifetime in my Rust code?

Avoid using the 'static lifetime unless your reference truly lives for the entire program duration. Overusing 'static to satisfy the borrow checker can mask design flaws and unnecessarily restrict the flexibility of your memory management.