Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use concrete names for concrete lifetimes in ch10-03 #3061

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
fn main() {
// ANCHOR: here
{
let r; // ---------+-- 'a
// |
{ // |
let x = 5; // -+-- 'b |
r = &x; // | |
} // -+ |
// |
println!("r: {}", r); // |
} // ---------+
let r; // -------------------+-- 'outer_scope
// |
{ // |
let x = 5; // -+-- 'inner_scope |
r = &x; // | |
} // -+ |
// |
println!("r: {}", r); // |
} // -------------------+
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
fn main() {
// ANCHOR: here
{
let x = 5; // ----------+-- 'b
// |
let r = &x; // --+-- 'a |
// | |
println!("r: {}", r); // | |
// --+ |
} // ----------+
let x = 5; // ----------------------+-- 'full_scope
// |
let r = &x; // --+-- 'partial_scope |
// | |
println!("r: {}", r); // | |
// --+ |
} // ----------------------+
// ANCHOR_END: here
}
16 changes: 8 additions & 8 deletions src/ch10-03-lifetime-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ whether all borrows are valid. Listing 10-18 shows the same code as Listing
```

<span class="caption">Listing 10-18: Annotations of the lifetimes of `r` and
`x`, named `'a` and `'b`, respectively</span>
`x`, named `'outer_scope` and `'inner_scope`, respectively</span>

Here, we’ve annotated the lifetime of `r` with `'a` and the lifetime of `x`
with `'b`. As you can see, the inner `'b` block is much smaller than the outer
`'a` lifetime block. At compile time, Rust compares the size of the two
lifetimes and sees that `r` has a lifetime of `'a` but that it refers to memory
with a lifetime of `'b`. The program is rejected because `'b` is shorter than
`'a`: the subject of the reference doesn’t live as long as the reference.
Here, we’ve annotated the lifetime of `r` with `'outer_scope` and the lifetime of `x`
with `'inner_scope`. As you can see, the `'inner_scope` block is much smaller than the
`'outer_scope` lifetime block. At compile time, Rust compares the size of the two
lifetimes and sees that `r` has a lifetime of `'outer_scope` but that it refers to memory
with a lifetime of `'inner_scope`. The program is rejected because `'inner_scope` is shorter than
`'outer_scope`: the subject of the reference doesn’t live as long as the reference.

Listing 10-19 fixes the code so it doesn’t have a dangling reference and
compiles without any errors.
Expand All @@ -86,7 +86,7 @@ compiles without any errors.
<span class="caption">Listing 10-19: A valid reference because the data has a
longer lifetime than the reference</span>

Here, `x` has the lifetime `'b`, which in this case is larger than `'a`. This
Here, `x` has the lifetime `'full_scope`, which in this case is larger than `'partial_scope`. This
means `r` can reference `x` because Rust knows that the reference in `r` will
always be valid while `x` is valid.

Expand Down