1
1
# Destructors
2
2
3
- When an [ initialized]   ; [ variable] or [ temporary] in Rust goes out of
3
+ When an [ initialized]   ; [ variable] or [ temporary] goes out of
4
4
[ scope] ( #drop-scopes ) its * destructor* is run, or it is * dropped* . [ Assignment]
5
5
also runs the destructor of its left-hand operand, if it's initialized. If a
6
6
variable has been partially initialized, only its initialized fields are
7
7
dropped.
8
8
9
- The destructor of a type ` T ` consists of
9
+ The destructor of a type ` T ` consists of:
10
10
11
11
1 . If ` T: Drop ` , calling [ ` <T as std::ops::Drop>::drop ` ]
12
12
2 . Recursively running the destructor of all of its fields.
@@ -41,7 +41,7 @@ let tuple = (ShowOnDrop("Tuple first"), ShowOnDrop("Tuple second"));
41
41
let moved ;
42
42
// No destructor run on assignment.
43
43
moved = ShowOnDrop (" Drops when moved" );
44
- // drops now, but is then uninitialized
44
+ // Drops now, but is then uninitialized.
45
45
moved ;
46
46
47
47
// Uninitialized does not drop.
@@ -60,7 +60,7 @@ Each variable or temporary is associated to a *drop scope*. When control flow
60
60
leaves a drop scope all variables associated to that scope are dropped in
61
61
reverse order of declaration (for variables) or creation (for temporaries).
62
62
63
- Drop scopes are determined after replacing [ ` for ` ] , [ ` if let ` ] and
63
+ Drop scopes are determined after replacing [ ` for ` ] , [ ` if let ` ] , and
64
64
[ ` while let ` ] expressions with the equivalent expressions using [ ` match ` ] .
65
65
Overloaded operators are not distinguished from built-in operators and [ binding
66
66
modes] are not considered.
@@ -87,17 +87,17 @@ from the inside outwards.
87
87
* The parent of a statement scope is the scope of the block that contains the
88
88
statement.
89
89
* The parent of the expression for a ` match ` guard is the scope of the arm that
90
- it's for.
90
+ the guard is for.
91
91
* The parent of the expression for a given ` match ` arm is that arm's scope.
92
- * The parent of the arm scope is the scope of the match expression that it
92
+ * The parent of the arm scope is the scope of the ` match ` expression that it
93
93
belongs to.
94
94
* The parent of all other scopes is the scope of the immediately enclosing
95
95
expression.
96
96
97
97
### Scopes of function parameters
98
98
99
99
All function parameters are in the scope of the entire function body, so are
100
- dropped last when evaluating the function. The actual function parameter is
100
+ dropped last when evaluating the function. Actual function parameters are
101
101
dropped after any named parameters that are bound to parts of it.
102
102
103
103
``` rust
@@ -124,7 +124,7 @@ patterns_in_parameters(
124
124
125
125
Local variables declared in a ` let ` statement are associated to the scope of
126
126
the block that contains the ` let ` statement. Local variables declared in a
127
- match are associated to the arm scope of the ` match ` arm that they are declared
127
+ ` match ` expression are associated to the arm scope of the ` match ` arm that they are declared
128
128
in.
129
129
130
130
``` rust
@@ -141,7 +141,7 @@ let declared_first = ShowOnDrop("Dropped last in outer scope");
141
141
let declared_last = ShowOnDrop (" Dropped first in outer scope" );
142
142
```
143
143
144
- If multiple patterns are used in the same arm for a match, then an unspecified
144
+ If multiple patterns are used in the same arm for a ` match ` expression , then an unspecified
145
145
pattern will be used to determine the drop order.
146
146
147
147
### Temporary scopes
@@ -256,7 +256,7 @@ let x = &mut 0;
256
256
println! (" {}" , x );
257
257
```
258
258
259
- If a borrow, dereference, field or tuple indexing expression has an extended
259
+ If a borrow, dereference, field, or tuple indexing expression has an extended
260
260
temporary scope then so does its operand. If an indexing expression has an
261
261
extended temporary scope then the indexed expression also has an extended
262
262
temporary scope.
@@ -267,7 +267,7 @@ An *extending pattern* is either
267
267
268
268
* An [ identifier pattern] that binds by reference or mutable reference.
269
269
* A [ struct] [ struct pattern ] , [ tuple] [ tuple pattern ] , [ tuple struct] [ tuple
270
- struct pattern] or [ slice] [ slice pattern ] pattern where at least one of the
270
+ struct pattern] , or [ slice] [ slice pattern ] pattern where at least one of the
271
271
direct subpatterns is a extending pattern.
272
272
273
273
So ` ref x ` , ` V(ref x) ` and ` [ref x, y] ` are all extending patterns, but ` x ` ,
@@ -282,13 +282,13 @@ For a let statement with an initializer, an *extending expression* is an
282
282
expression which is one of the following:
283
283
284
284
* The initializer expression.
285
- * The operand of a extending [ borrow expression] .
285
+ * The operand of an extending [ borrow expression] .
286
286
* The operand(s) of an extending [ array] [ array expression ] , [ cast] [ cast
287
- expression] , [ braced struct] [ struct expression ] or [ tuple] [ tuple expression ]
287
+ expression] , [ braced struct] [ struct expression ] , or [ tuple] [ tuple expression ]
288
288
expression.
289
289
* The final expression of any extending [ block expression] [ block expressions ] .
290
290
291
- So the borrow expressions in ` &mut 0 ` , ` (&1, &mut 2) ` and ` Some { 0: &mut 3 } `
291
+ So the borrow expressions in ` &mut 0 ` , ` (&1, &mut 2) ` , and ` Some { 0: &mut 3 } `
292
292
are all extending expressions, while the borrows in ` &0 + &1 ` and
293
293
` Some(&mut 0) ` are not.
294
294
@@ -320,7 +320,7 @@ Here are some examples where expressions don't have extended temporary scopes:
320
320
# fn temp() {}
321
321
# trait Use { fn use_temp(&self) -> &Self { self } }
322
322
# impl Use for () {}
323
- // The temporary that stores the result of `temp()` lives only lives until the
323
+ // The temporary that stores the result of `temp()` only lives until the
324
324
// end of the let statement in these cases.
325
325
326
326
let x = Some(&temp()); // ERROR
0 commit comments