From 04317e3cbb2a4ed0aecb03d932ee49435322c626 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Thu, 3 Apr 2025 08:39:47 -0700 Subject: [PATCH 1/2] Mention the temporary scope of `while let` I'm not an expert here, but because `while let` is a loop, it *has to* drop temporaries when it loops, so it must have at least one temporary scope, and empirically, the same scope includes the scrutinee and the body because the body can use temporaries from the scrutinee. --- src/destructors.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/destructors.md b/src/destructors.md index 2f4711815..41490a869 100644 --- a/src/destructors.md +++ b/src/destructors.md @@ -208,6 +208,7 @@ smallest scope that contains the expression and is one of the following: * The body expression for a match arm. * Each operand of a [lazy boolean expression]. * The pattern-matching condition and consequent body of [`if let`] ([destructors.scope.temporary.edition2024]). +* The pattern-matching condition and loop body of [`while let`]. * The entirety of the tail expression of a block ([destructors.scope.temporary.edition2024]). > [!NOTE] From bcfbb989398ea5744dd20bbde7d328d761473462 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Thu, 8 May 2025 16:49:36 +0000 Subject: [PATCH 2/2] Add example for temp scope of `while let` --- src/destructors.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/destructors.md b/src/destructors.md index 41490a869..71907c5dd 100644 --- a/src/destructors.md +++ b/src/destructors.md @@ -221,6 +221,7 @@ r[destructors.scope.temporary.edition2024] Some examples: ```rust +# #![allow(irrefutable_let_patterns)] # struct PrintOnDrop(&'static str); # impl Drop for PrintOnDrop { # fn drop(&mut self) { @@ -247,6 +248,13 @@ else { // `if let else` dropped here }; +while let x = PrintOnDrop("while let scrutinee").0 { + PrintOnDrop("while let loop body").0; + break; + // `while let loop body` dropped here. + // `while let scrutinee` dropped here. +} + // Dropped before the first || (PrintOnDrop("first operand").0 == "" // Dropped before the )