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
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
