* 粗口大师班——第20章:高级生平

2026年9月3日2 次浏览来源:Dev.to阅读原文

正文保留英文原文(机翻易破坏代码与排版),标题/摘要已提供中文

🦀 Rust Master Class - Chapter 20: Advanced Lifetimes Visiting my grandfather's grave.

The time we had wasn't enough, but it was what we had.

Lifetimes in Rust aren't about duration — they're about knowing what outlives what.

Advanced lifetimes in Rust extend beyond basic function signatures to handle complex reference relationships, subtyping, and memory safety constraints.

Based on the sources, the key concepts include the outlives relationship, lifetime bounds, variance, and anonymous lifetimes.

1.

The Outlives Relationship The foundational concept in advanced lifetimes is the outlives relationship.

If one lifetime lasts at least as long as another lifetime , it is written as .

This relationship is essential for functions that take multiple references and need to guarantee that one piece of data does not expire before another that depends on it .

2.

Lifetime Bounds Lifetime bounds can be applied to generic types.

The bound means that the type (and any references it contains) must last at least as long as the lifetime .

Implied Bounds: In many cases, Rust assumes these bounds.

For example, in a struct , if it contains a field , the bound is implied because the reference would be invalid if didn't last at least as long as .

Code Example: [Source: 120, 121]

3.

Subtyping and Variance Variance describes how the subtyping relationship between lifetimes (e.g., outliving ) translates to the subtyping relationship between complex types .

Covariance: If , then is a subtype of .

Example: Because outlives , an immutable reference is a subtype of and can be passed to functions expecting the shorter lifetime .

Contravariance: If , then is a subtype of .

Example: This primarily occurs in function arguments.

A function type is a subtype of because a function that can handle a shorter lifetime can safely handle a longer one .

Invariance: If , then has no relationship with .

Example: Mutable references () are invariant.

If they were covariant, you could accidentally store a short-lived reference into a long-lived container, causing a dangling pointer .

Code Example (Invariance Error): [Source: 124, 129]

4.

Anonymous Lifetimes () Anonymous lifetimes are used when you want the compiler to infer (guess) the lifetime .

This is allowed only when there is exactly one logical choice for the compiler to make .

Common Use Case: Inside an block, using tells the compiler the lifetime must match the lifetime of .

Code Example: [Source: 125]

5.

Lifetime Elision Rules Advanced Rust relies on three rules to elide (omit) lifetimes in function signatures automatically: Each reference parameter gets its own unique lifetime parameter .

If there is exactly one input lifetime, that lifetime is assigned to all output references .

If there are multiple input lifetimes but one is or , the lifetime of is assigned to all output references .

If a function does not satisfy these rules, lifetimes must be specified explicitly . 📖 Download the full PDF: https://drive.google.com/file/d/1GQGz8ztwPG4iqAgTDQ11Iw-YJXeKX6W2/view?usp=sharing Part 20 of the Rust Master Class series — STEM EdTech | Automation Consulting | Rust Tutoring RustLang #Programming #LearnToCode #STEM #EdTech

分享