Skip to content

Commit

Permalink
Include notes on when to use lifetime annotations (#1616)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djmitche committed Dec 31, 2023
1 parent 9b439b3 commit 09aa70b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/slices-and-lifetimes/lifetime-elision.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 09aa70b

Please sign in to comment.