|
1 | 1 | # Lifetimes
|
2 | 2 |
|
3 |
| -A *lifetime* is a construct the compiler (also called the borrow checker) |
4 |
| -uses to ensure all borrows are valid. Specifically, a variable's lifetime |
5 |
| -begins when it is created and ends when it is destroyed. While lifetimes |
6 |
| -and scopes are often referred to together, they are not the same. |
| 3 | +A *lifetime* is a construct the compiler (or more specifically, its *borrow |
| 4 | +checker*) uses to ensure all borrows are valid. Specifically, a variable's |
| 5 | +lifetime begins when it is created and ends when it is destroyed. While |
| 6 | +lifetimes and scopes are often referred to together, they are not the same. |
7 | 7 |
|
8 |
| -Take, for example, the case where we borrow a variable via `&`. The |
9 |
| -borrow has a lifetime that is determined by where it is declared. As a result, |
10 |
| -the borrow is valid as long as it ends before the lender is destroyed. However, |
| 8 | +Take, for example, the case where we borrow a variable via `&`. The |
| 9 | +borrow has a lifetime that is determined by where it is declared. As a result, |
| 10 | +the borrow is valid as long as it ends before the lender is destroyed. However, |
11 | 11 | the scope of the borrow is determined by where the reference is used.
|
12 | 12 |
|
13 |
| -In the following example and in the rest of this section, we will see how |
| 13 | +In the following example and in the rest of this section, we will see how |
14 | 14 | lifetimes relate to scopes, as well as how the two differ.
|
15 | 15 |
|
16 | 16 | ```rust,editable
|
|
0 commit comments