Skip to content
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
2 changes: 1 addition & 1 deletion src/lifetimes/borrow-both.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ In this case, we have a function where either `a` or `b` may be returned. In
this case we use the lifetime annotations to tell the compiler that both borrows
may flow into the return value.

```rust
```rust,editable
fn pick<'a>(c: bool, a: &'a i32, b: &'a i32) -> &'a i32 {
if c { a } else { b }
}
Expand Down
16 changes: 11 additions & 5 deletions src/lifetimes/borrow-one.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ fn main() {
enforcing that the function adheres to the contract set by the function's
signature.

- The "help" note in the error notes that we can add a lifetime bound `'b: 'a`
to say that `'b` will live at least as long as `'a`, which would then allow us
to return `query`. On the next slide we'll talk about lifetime variance, which
is the rule that allows us to return a longer lifetime when a shorter one is
expected.
# More to Explore

- The "help" message in the error notes that we can add a lifetime bound
`'b: 'a` to say that `'b` will live at least as long as `'a`, which would then
allow us to return `query`. This is an example of lifetime "variance", which
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is lifetime subtyping; variance only has to do with the way the subtyping relation is reversed for function argument types relative to function return types.

allows us to return a longer lifetime where a shorter one is expected.

- We can do something similar by returning a `'static` lifetime, e.g., a
reference to a `static` variable. The `'static` lifetime is guaranteed to be
longer than any other lifetime, so it's always safe to return in place of a
shorter lifetime.

</details>
2 changes: 1 addition & 1 deletion src/lifetimes/lifetime-elision.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
minutes: 5
---

# Lifetimes in Function Calls
# Lifetimes Elision
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "Lifetime Elision" (singular).


Lifetimes for function arguments and return values must be fully specified, but
Rust allows lifetimes to be elided in most cases with
Expand Down
2 changes: 1 addition & 1 deletion src/lifetimes/multiple-borrows.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minutes: 5
But what about when there are multiple borrows passed into a function and one
being returned?

```rust,editable,ignore
```rust,editable,compile_fail
fn multiple(a: &i32, b: &i32) -> &i32 {
todo!("Return either `a` or `b`")
}
Expand Down