liveBook · Manning

Lifetime graph

As explained in Refactoring to Rust

A lifetime graph is a visualization that represents when values or variables are created, usable, moved, and destroyed.

A lifetime graph visualizes when a Rust value or variable is created, usable, moved, and destroyed. It makes memory-management problems easier to trace. section 2.3

Read a lifetime graph by tracking these events:

  • A value becomes usable after its memory is allocated and remains valid until it is dropped. section 2.3
  • A move transfers ownership and ends the original variable’s usable lifetime. section 2.3
  • A reference must not outlive the value it points to; the graph exposes invalid borrows and dangling references. section 2.3.3

Tracing ownership and references

If main creates art1 and passes it to admire_art, art1’s lifetime disappears from main when the value moves. If borrowed_art refers to art1, using it afterward is invalid. Likewise, build_art cannot return a reference to local art because art is dropped when the function ends. section 2.3 section 2.3.3

Figure 2.1 The lifetime graph for listing 2.2
figure

Use the lifetime figures to compare an ordinary variable lifetime, an ownership move, coexisting variables, and references extending beyond their values. section 2.3 section 2.3.3

How Refactoring to Rust covers it

How deep it goes
Explains creation, usability, moves, drops, ownership transfers, and invalid references through concrete graph traces.
Examples
Uses Artwork values, art1, admire_art, borrowed_art, build_art, and lifetime figures to expose moves and invalid references.
What it assumes
Builds from values, scope, ownership, moves, and references while defining validity through visual timelines.
What you can do after
Diagnose ownership and borrowing errors by locating moves, drops, and references that extend beyond valid values.
How it connects
Connects lifetimes with ownership, moves, scope, references, borrowing, and compiler validity checks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.