Skip to content

Commit 8222c70

Browse files
authored
Minor fixes in new lifetimes section (#2965)
@gribozavr I had a couple more fixes for #2964 that I didn't push until after you merged the branch.
1 parent bb4db3d commit 8222c70

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/lifetimes/borrow-both.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In this case, we have a function where either `a` or `b` may be returned. In
88
this case we use the lifetime annotations to tell the compiler that both borrows
99
may flow into the return value.
1010

11-
```rust
11+
```rust,editable
1212
fn pick<'a>(c: bool, a: &'a i32, b: &'a i32) -> &'a i32 {
1313
if c { a } else { b }
1414
}

src/lifetimes/borrow-one.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ fn main() {
6868
enforcing that the function adheres to the contract set by the function's
6969
signature.
7070

71-
- The "help" note in the error notes that we can add a lifetime bound `'b: 'a`
72-
to say that `'b` will live at least as long as `'a`, which would then allow us
73-
to return `query`. On the next slide we'll talk about lifetime variance, which
74-
is the rule that allows us to return a longer lifetime when a shorter one is
75-
expected.
71+
# More to Explore
72+
73+
- The "help" message in the error notes that we can add a lifetime bound
74+
`'b: 'a` to say that `'b` will live at least as long as `'a`, which would then
75+
allow us to return `query`. This is an example of lifetime subtyping, which
76+
allows us to return a longer lifetime where a shorter one is expected.
77+
78+
- We can do something similar by returning a `'static` lifetime, e.g., a
79+
reference to a `static` variable. The `'static` lifetime is guaranteed to be
80+
longer than any other lifetime, so it's always safe to return in place of a
81+
shorter lifetime.
7682

7783
</details>

src/lifetimes/lifetime-elision.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
minutes: 5
33
---
44

5-
# Lifetimes in Function Calls
5+
# Lifetime Elision
66

77
Lifetimes for function arguments and return values must be fully specified, but
88
Rust allows lifetimes to be elided in most cases with

src/lifetimes/multiple-borrows.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ minutes: 5
77
But what about when there are multiple borrows passed into a function and one
88
being returned?
99

10-
```rust,editable,ignore
10+
```rust,editable,compile_fail
1111
fn multiple(a: &i32, b: &i32) -> &i32 {
1212
todo!("Return either `a` or `b`")
1313
}

0 commit comments

Comments
 (0)