1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-18 04:23:14 +02:00

Include notes on when to use lifetime annotations (#1616)

This addresses 

> Could use more clarity on why or when people would want to use
lifetimes -- when is it worth going to the trouble of not Cloning, etc.

in #1565.
This commit is contained in:
Dustin J. Mitchell 2023-12-31 10:03:05 -05:00 committed by GitHub
parent 9b439b34ab
commit 09aa70b963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,4 +68,10 @@ This won't compile, demonstrating that the annotations are checked for validity
by the compiler. Note that this is not the case for raw pointers (unsafe), and
this is a common source of errors with unsafe Rust.
Students may ask when to use lifetimes. Rust borrows _always_ have lifetimes.
Most of the time, elision and type inference mean these don't need to be
written out. In more complicated cases, lifetime annotations can help resolve
ambiguity. Often, especially when prototyping, it's easier to just work with
owned data by cloning values where necessary.
</details>