@@ -67,7 +67,8 @@ Given a function, or closure, there are drop scopes for:
67
67
* Each [ statement]
68
68
* Each [ expression]
69
69
* Each block, including the function body
70
- * [ Block expressions] the expression and block scopes are the same scope.
70
+ * In the case of a [ block expression] , the scope for the block and the
71
+ expression are the same scope.
71
72
* Each arm of a ` match ` expression
72
73
73
74
Drop scopes are nested within one another as follows. When multiple scopes are
@@ -78,13 +79,14 @@ from the inside outwards.
78
79
* The function body block is contained within the scope of the entire function.
79
80
* The parent of the expression in an expression statement is the scope of the
80
81
statement.
81
- * The parent of the initializer of a ` let ` statement is the ` let `
82
- statement's scope.
82
+ * The parent of the initializer of a [ ` let ` statement] is the ` let ` statement's
83
+ scope.
83
84
* The parent of a statement scope is the scope of the block that contains the
84
85
statement.
85
86
* The parent of the expression for a ` match ` guard is the scope of the arm that
86
87
the guard is for.
87
- * The parent of the expression for a given ` match ` arm is that arm's scope.
88
+ * The parent of the expression after the ` => ` in a ` match ` expression is the
89
+ scope of the arm that it's in.
88
90
* The parent of the arm scope is the scope of the ` match ` expression that it
89
91
belongs to.
90
92
* The parent of all other scopes is the scope of the immediately enclosing
@@ -93,8 +95,8 @@ from the inside outwards.
93
95
### Scopes of function parameters
94
96
95
97
All function parameters are in the scope of the entire function body, so are
96
- dropped last when evaluating the function. Actual function parameters are
97
- dropped after any named parameters that are bound to parts of it .
98
+ dropped last when evaluating the function. Each actual function parameter is
99
+ dropped after any bindings introduced in that parameter's pattern .
98
100
99
101
``` rust
100
102
# struct PrintOnDrop (& 'static str );
@@ -120,8 +122,8 @@ patterns_in_parameters(
120
122
121
123
Local variables declared in a ` let ` statement are associated to the scope of
122
124
the block that contains the ` let ` statement. Local variables declared in a
123
- ` match ` expression are associated to the arm scope of the ` match ` arm that they are declared
124
- in.
125
+ ` match ` expression are associated to the arm scope of the ` match ` arm that they
126
+ are declared in.
125
127
126
128
``` rust
127
129
# struct PrintOnDrop (& 'static str );
@@ -137,14 +139,14 @@ let declared_first = PrintOnDrop("Dropped last in outer scope");
137
139
let declared_last = PrintOnDrop (" Dropped first in outer scope" );
138
140
```
139
141
140
- If multiple patterns are used in the same arm for a ` match ` expression, then an unspecified
141
- pattern will be used to determine the drop order.
142
+ If multiple patterns are used in the same arm for a ` match ` expression, then an
143
+ unspecified pattern will be used to determine the drop order.
142
144
143
145
### Temporary scopes
144
146
145
147
The * temporary scope* of an expression is the scope that is used for the
146
148
temporary variable that holds the result of that expression when used in a
147
- [ place context] , unless it is promoted to a ` static ` .
149
+ [ place context] , unless it is [ promoted] .
148
150
149
151
Apart from lifetime extension, the temporary scope of an expression is the
150
152
smallest scope that contains the expression and is for one of the following:
@@ -158,9 +160,16 @@ smallest scope that contains the expression and is for one of the following:
158
160
* The expression for a match arm.
159
161
* The second operand of a [ lazy boolean expression] .
160
162
161
- > Note: Temporaries that are created in the final expression of a function body
162
- > are dropped * after* any named variables bound in the function body, as there
163
- > is no smaller enclosing temporary scope.
163
+ > ** Notes** :
164
+ >
165
+ > Temporaries that are created in the final expression of a function
166
+ > body are dropped * after* any named variables bound in the function body, as
167
+ > there is no smaller enclosing temporary scope.
168
+ >
169
+ > The [ scrutinee] of a ` match ` expression is not a temporary scope, so
170
+ > temporaries in the scrutinee can be dropped after the ` match ` expression. For
171
+ > example, the temporary for ` 1 ` in ` match 1 { ref mut z => z }; ` lives until
172
+ > the end of the statement.
164
173
165
174
Some examples:
166
175
@@ -241,6 +250,9 @@ always has the type `&'static Option<_>`, as it contains nothing disallowed).
241
250
242
251
### Temporary lifetime extension
243
252
253
+ > ** Note** : The exact rules for temporary lifetime extension are subject to
254
+ > change. This is describes the current behavior only.
255
+
244
256
The temporary scopes for expressions in ` let ` statements are sometimes
245
257
* extended* to the scope of the block containing the ` let ` statement. This is
246
258
done when the usual temporary scope would be too small, based on certain
@@ -283,11 +295,11 @@ expression which is one of the following:
283
295
* The operand(s) of an extending [ array] [ array expression ] , [ cast] [ cast
284
296
expression] , [ braced struct] [ struct expression ] , or [ tuple] [ tuple expression ]
285
297
expression.
286
- * The final expression of any extending [ block expression] [ block expressions ] .
298
+ * The final expression of any extending [ block expression] .
287
299
288
300
So the borrow expressions in ` &mut 0 ` , ` (&1, &mut 2) ` , and ` Some { 0: &mut 3 } `
289
- are all extending expressions, while the borrows in ` &0 + &1 ` and
290
- ` Some(&mut 0) ` are not .
301
+ are all extending expressions. The borrows in ` &0 + &1 ` and ` Some(&mut 0) ` are
302
+ not: the latter is syntactically a function call expression .
291
303
292
304
The operand of any extending borrow expression has its temporary scope
293
305
extended.
@@ -341,6 +353,8 @@ variable or field from being dropped automatically.
341
353
[ interior mutability ] : interior-mutability.md
342
354
[ lazy boolean expression ] : expressions/operator-expr.md#lazy-boolean-operators
343
355
[ place context ] : expressions.md#place-expressions-and-value-expressions
356
+ [ promoted ] : destructors.md#constant-promotion
357
+ [ scrutinee ] : glossary.md#scrutinee
344
358
[ statement ] : statements.md
345
359
[ temporary ] : expressions.md#temporaries
346
360
[ variable ] : variables.md
@@ -358,7 +372,7 @@ variable or field from being dropped automatically.
358
372
[ tuple struct pattern ] : patterns.md#tuple-struct-patterns
359
373
360
374
[ array expression ] : expressions/array-expr.md#array-expressions
361
- [ block expressions ] : expressions/block-expr.md
375
+ [ block expression ] : expressions/block-expr.md
362
376
[ borrow expression ] : expressions/operator-expr.md#borrow-operators
363
377
[ cast expression ] : expressions/operator-expr.md#type-cast-expressions
364
378
[ struct expression ] : expressions/struct-expr.md
0 commit comments