1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-16 14:53:28 +02:00
comprehensive-rust/src/ownership/lifetimes.md
2023-06-22 13:47:27 +01:00

689 B

Lifetimes

A borrowed value has a lifetime:

  • The lifetime can be implicit: add(p1: &Point, p2: &Point) -> Point.
  • Lifetimes can also be explicit: &'a Point, &'document str.
  • Read &'a Point as "a borrowed Point which is valid for at least the lifetime a".
  • Lifetimes are always inferred by the compiler: you cannot assign a lifetime yourself.
    • Lifetime annotations create constraints; the compiler verifies that there is a valid solution.
  • Lifetimes for function arguments and return values must be fully specified, but Rust allows lifetimes to be elided in most cases with a few simple rules.