From 00f17a9222c71cbd5c26da18226248bdbc653845 Mon Sep 17 00:00:00 2001 From: aviad-dev <117904293+aviad-dev@users.noreply.github.com> Date: Thu, 9 Nov 2023 16:50:58 +0200 Subject: [PATCH 1/2] rename `y` to `_y` to get the correct compile error Without this change, the error is: ``` Compiling playground v0.0.1 (/playground) warning: unused variable: `y` --> src/main.rs:13:9 | 13 | let y: &'a i32 = &_x; | ^ help: if this is intentional, prefix it with an underscore: `_y` | = note: `#[warn(unused_variables)]` on by default error[E0597]: `_x` does not live long enough --> src/main.rs:13:22 | 9 | fn failed_borrow<'a>() { | -- lifetime `'a` defined here 10 | let _x = 12; | -- binding `_x` declared here ... 13 | let y: &'a i32 = &_x; | ------- ^^^ borrowed value does not live long enough | | | type annotation requires that `_x` is borrowed for `'a` ... 17 | } | - `_x` dropped here while still borrowed For more information about this error, try `rustc --explain E0597`. warning: `playground` (bin "playground") generated 1 warning error: could not compile `playground` (bin "playground") due to previous error; 1 warning emitted ``` --- src/scope/lifetime/explicit.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scope/lifetime/explicit.md b/src/scope/lifetime/explicit.md index f0ecdf5fca..b058c14b02 100644 --- a/src/scope/lifetime/explicit.md +++ b/src/scope/lifetime/explicit.md @@ -40,7 +40,7 @@ fn failed_borrow<'a>() { let _x = 12; // ERROR: `_x` does not live long enough - let y: &'a i32 = &_x; + let _y: &'a i32 = &_x; // Attempting to use the lifetime `'a` as an explicit type annotation // inside the function will fail because the lifetime of `&_x` is shorter // than that of `y`. A short lifetime cannot be coerced into a longer one. From 3dffa1e8327ea1e6a1351bd291e31a21ad285444 Mon Sep 17 00:00:00 2001 From: aviad-dev <117904293+aviad-dev@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:53:16 +0200 Subject: [PATCH 2/2] Code review change request --- src/scope/lifetime/explicit.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scope/lifetime/explicit.md b/src/scope/lifetime/explicit.md index b058c14b02..68d144a82c 100644 --- a/src/scope/lifetime/explicit.md +++ b/src/scope/lifetime/explicit.md @@ -43,7 +43,7 @@ fn failed_borrow<'a>() { let _y: &'a i32 = &_x; // Attempting to use the lifetime `'a` as an explicit type annotation // inside the function will fail because the lifetime of `&_x` is shorter - // than that of `y`. A short lifetime cannot be coerced into a longer one. + // than that of `_y`. A short lifetime cannot be coerced into a longer one. } fn main() {