Skip to content

Commit

Permalink
Merge pull request #1901 from L0uisc/lifetime-explanation
Browse files Browse the repository at this point in the history
Make lifetime explanation clearer
  • Loading branch information
carols10cents committed Apr 15, 2019
2 parents 2797e27 + e59c9f8 commit 5fa10e2
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/ch10-03-lifetime-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ The function signature now tells Rust that for some lifetime `'a`, the function
takes two parameters, both of which are string slices that live at least as
long as lifetime `'a`. The function signature also tells Rust that the string
slice returned from the function will live at least as long as lifetime `'a`.
These constraints are what we want Rust to enforce. Remember, when we specify
the lifetime parameters in this function signature, we’re not changing the
lifetimes of any values passed in or returned. Rather, we’re specifying that
the borrow checker should reject any values that don’t adhere to these
constraints. Note that the `longest` function doesn’t need to know exactly how
long `x` and `y` will live, only that some scope can be substituted for `'a`
that will satisfy this signature.
In practice, it means that the lifetime of the reference returned by the
`longest` function is the same as the smaller of the lifetimes of the
references passed in. These constraints are what we want Rust to enforce.
Remember, when we specify the lifetime parameters in this function signature,
we’re not changing the lifetimes of any values passed in or returned. Rather,
we’re specifying that the borrow checker should reject any values that don’t
adhere to these constraints. Note that the `longest` function doesn’t need to
know exactly how long `x` and `y` will live, only that some scope can be
substituted for `'a` that will satisfy this signature.

When annotating lifetimes in functions, the annotations go in the function
signature, not in the function body. Rust can analyze the code within the
Expand Down

0 comments on commit 5fa10e2

Please sign in to comment.