From afcbfc09e2c13dda1083be73c4191caf5fb16828 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Fri, 31 Oct 2025 12:50:45 -0700 Subject: [PATCH 1/4] Fix various minor things --- src/lifetimes/borrow-both.md | 2 +- src/lifetimes/borrow-one.md | 16 +++++++++++----- src/lifetimes/lifetime-elision.md | 2 +- src/lifetimes/multiple-borrows.md | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/lifetimes/borrow-both.md b/src/lifetimes/borrow-both.md index 4191f0886e96..b6561a33ae4f 100644 --- a/src/lifetimes/borrow-both.md +++ b/src/lifetimes/borrow-both.md @@ -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 } } diff --git a/src/lifetimes/borrow-one.md b/src/lifetimes/borrow-one.md index ae9fe679821c..9671ec205845 100644 --- a/src/lifetimes/borrow-one.md +++ b/src/lifetimes/borrow-one.md @@ -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 + 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. diff --git a/src/lifetimes/lifetime-elision.md b/src/lifetimes/lifetime-elision.md index 5094724e9c3d..7637757a90c8 100644 --- a/src/lifetimes/lifetime-elision.md +++ b/src/lifetimes/lifetime-elision.md @@ -2,7 +2,7 @@ minutes: 5 --- -# Lifetimes in Function Calls +# Lifetimes Elision Lifetimes for function arguments and return values must be fully specified, but Rust allows lifetimes to be elided in most cases with diff --git a/src/lifetimes/multiple-borrows.md b/src/lifetimes/multiple-borrows.md index 8d1e4680f79b..95b0135914d9 100644 --- a/src/lifetimes/multiple-borrows.md +++ b/src/lifetimes/multiple-borrows.md @@ -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`") } From 8a2f06ee7fb49f8b67f318222dbdab9c0964b471 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Fri, 31 Oct 2025 13:11:16 -0700 Subject: [PATCH 2/4] Fix formatting --- src/lifetimes/borrow-one.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lifetimes/borrow-one.md b/src/lifetimes/borrow-one.md index 9671ec205845..bbaad0c4fb31 100644 --- a/src/lifetimes/borrow-one.md +++ b/src/lifetimes/borrow-one.md @@ -70,10 +70,11 @@ fn main() { # 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 - allows us to return a longer lifetime where a shorter one is expected. +- 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 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 From d7e56db9bbeb4e2d8a7065130cd6b307b99dada9 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Tue, 4 Nov 2025 11:07:47 -0800 Subject: [PATCH 3/4] Minor code review changes --- src/lifetimes/borrow-one.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lifetimes/borrow-one.md b/src/lifetimes/borrow-one.md index bbaad0c4fb31..922223a30466 100644 --- a/src/lifetimes/borrow-one.md +++ b/src/lifetimes/borrow-one.md @@ -71,12 +71,11 @@ fn main() { # 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 allows us to return a longer lifetime where a shorter one is expected. + `'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 + 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 +- 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. From 723428c5fcdb9d0872406a3e10902276025c1f0e Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 13 Nov 2025 10:49:29 -0800 Subject: [PATCH 4/4] Typo fixes --- src/lifetimes/borrow-one.md | 2 +- src/lifetimes/lifetime-elision.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lifetimes/borrow-one.md b/src/lifetimes/borrow-one.md index 922223a30466..c99096cd6512 100644 --- a/src/lifetimes/borrow-one.md +++ b/src/lifetimes/borrow-one.md @@ -72,7 +72,7 @@ fn main() { - 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 + allow us to return `query`. This is an example of lifetime subtyping, which 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 diff --git a/src/lifetimes/lifetime-elision.md b/src/lifetimes/lifetime-elision.md index 7637757a90c8..9e3219916251 100644 --- a/src/lifetimes/lifetime-elision.md +++ b/src/lifetimes/lifetime-elision.md @@ -2,7 +2,7 @@ minutes: 5 --- -# Lifetimes Elision +# Lifetime Elision Lifetimes for function arguments and return values must be fully specified, but Rust allows lifetimes to be elided in most cases with