@@ -71,7 +71,8 @@ Given a function, or closure, there are drop scopes for:
71
71
* Each [ statement]
72
72
* Each [ expression]
73
73
* Each block, including the function body
74
- * [ Block expressions] the expression and block scopes are the same scope.
74
+ * In the case of a [ block expression] , the scope for the block and the
75
+ expression are the same scope.
75
76
* Each arm of a ` match ` expression
76
77
77
78
Drop scopes are nested within one another as follows. When multiple scopes are
@@ -82,13 +83,14 @@ from the inside outwards.
82
83
* The function body block is contained within the scope of the entire function.
83
84
* The parent of the expression in an expression statement is the scope of the
84
85
statement.
85
- * The parent of the initializer of a ` let ` statement is the ` let `
86
- statement's scope.
86
+ * The parent of the initializer of a [ ` let ` statement] is the ` let ` statement's
87
+ scope.
87
88
* The parent of a statement scope is the scope of the block that contains the
88
89
statement.
89
90
* The parent of the expression for a ` match ` guard is the scope of the arm that
90
91
the guard is for.
91
- * The parent of the expression for a given ` match ` arm is that arm's scope.
92
+ * The parent of the expression after the ` => ` in a ` match ` expression is the
93
+ scope of the arm that it's in.
92
94
* The parent of the arm scope is the scope of the ` match ` expression that it
93
95
belongs to.
94
96
* The parent of all other scopes is the scope of the immediately enclosing
@@ -97,8 +99,8 @@ from the inside outwards.
97
99
### Scopes of function parameters
98
100
99
101
All function parameters are in the scope of the entire function body, so are
100
- dropped last when evaluating the function. Actual function parameters are
101
- dropped after any named parameters that are bound to parts of it .
102
+ dropped last when evaluating the function. Each actual function parameter is
103
+ dropped after any bindings introduced in that parameter's pattern .
102
104
103
105
``` rust
104
106
# struct PrintOnDrop (& 'static str );
@@ -124,8 +126,8 @@ patterns_in_parameters(
124
126
125
127
Local variables declared in a ` let ` statement are associated to the scope of
126
128
the block that contains the ` let ` statement. Local variables declared in a
127
- ` match ` expression are associated to the arm scope of the ` match ` arm that they are declared
128
- in.
129
+ ` match ` expression are associated to the arm scope of the ` match ` arm that they
130
+ are declared in.
129
131
130
132
``` rust
131
133
# struct PrintOnDrop (& 'static str );
@@ -141,14 +143,14 @@ let declared_first = PrintOnDrop("Dropped last in outer scope");
141
143
let declared_last = PrintOnDrop (" Dropped first in outer scope" );
142
144
```
143
145
144
- If multiple patterns are used in the same arm for a ` match ` expression, then an unspecified
145
- pattern will be used to determine the drop order.
146
+ If multiple patterns are used in the same arm for a ` match ` expression, then an
147
+ unspecified pattern will be used to determine the drop order.
146
148
147
149
### Temporary scopes
148
150
149
151
The * temporary scope* of an expression is the scope that is used for the
150
152
temporary variable that holds the result of that expression when used in a
151
- [ place context] , unless it is promoted to a ` static ` .
153
+ [ place context] , unless it is [ promoted] .
152
154
153
155
Apart from lifetime extension, the temporary scope of an expression is the
154
156
smallest scope that contains the expression and is for one of the following:
@@ -162,9 +164,16 @@ smallest scope that contains the expression and is for one of the following:
162
164
* The expression for a match arm.
163
165
* The second operand of a [ lazy boolean expression] .
164
166
165
- > Note: Temporaries that are created in the final expression of a function body
166
- > are dropped * after* any named variables bound in the function body, as there
167
- > is no smaller enclosing temporary scope.
167
+ > ** Notes** :
168
+ >
169
+ > Temporaries that are created in the final expression of a function
170
+ > body are dropped * after* any named variables bound in the function body, as
171
+ > there is no smaller enclosing temporary scope.
172
+ >
173
+ > The [ scrutinee] of a ` match ` expression is not a temporary scope, so
174
+ > temporaries in the scrutinee can be dropped after the ` match ` expression. For
175
+ > example, the temporary for ` 1 ` in ` match 1 { ref mut z => z }; ` lives until
176
+ > the end of the statement.
168
177
169
178
Some examples:
170
179
@@ -245,6 +254,9 @@ always has the type `&'static Option<_>`, as it contains nothing disallowed).
245
254
246
255
### Temporary lifetime extension
247
256
257
+ > ** Note** : The exact rules for temporary lifetime extension are subject to
258
+ > change. This is describes the current behavior only.
259
+
248
260
The temporary scopes for expressions in ` let ` statements are sometimes
249
261
* extended* to the scope of the block containing the ` let ` statement. This is
250
262
done when the usual temporary scope would be too small, based on certain
@@ -287,11 +299,11 @@ expression which is one of the following:
287
299
* The operand(s) of an extending [ array] [ array expression ] , [ cast] [ cast
288
300
expression] , [ braced struct] [ struct expression ] , or [ tuple] [ tuple expression ]
289
301
expression.
290
- * The final expression of any extending [ block expression] [ block expressions ] .
302
+ * The final expression of any extending [ block expression] .
291
303
292
304
So the borrow expressions in ` &mut 0 ` , ` (&1, &mut 2) ` , and ` Some { 0: &mut 3 } `
293
- are all extending expressions, while the borrows in ` &0 + &1 ` and
294
- ` Some(&mut 0) ` are not .
305
+ are all extending expressions. The borrows in ` &0 + &1 ` and ` Some(&mut 0) ` are
306
+ not: the latter is syntactically a function call expression .
295
307
296
308
The operand of any extending borrow expression has its temporary scope
297
309
extended.
@@ -345,6 +357,8 @@ variable or field from being dropped automatically.
345
357
[ interior mutability ] : interior-mutability.md
346
358
[ lazy boolean expression ] : expressions/operator-expr.md#lazy-boolean-operators
347
359
[ place context ] : expressions.md#place-expressions-and-value-expressions
360
+ [ promoted ] : destructors.md#constant-promotion
361
+ [ scrutinee ] : glossary.md#scrutinee
348
362
[ statement ] : statements.md
349
363
[ temporary ] : expressions.md#temporaries
350
364
[ variable ] : variables.md
@@ -362,7 +376,7 @@ variable or field from being dropped automatically.
362
376
[ tuple struct pattern ] : patterns.md#tuple-struct-patterns
363
377
364
378
[ array expression ] : expressions/array-expr.md#array-expressions
365
- [ block expressions ] : expressions/block-expr.md
379
+ [ block expression ] : expressions/block-expr.md
366
380
[ borrow expression ] : expressions/operator-expr.md#borrow-operators
367
381
[ cast expression ] : expressions/operator-expr.md#type-cast-expressions
368
382
[ struct expression ] : expressions/struct-expr.md
0 commit comments