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,10 +41,11 @@ 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
let uninitialized : ShowOnDrop ;
47
- // Only the first element is dropped
48
+ // Only the first element is dropped.
48
49
let mut partially_initialized : (ShowOnDrop , ShowOnDrop );
49
50
partially_initialized . 0 = ShowOnDrop (" Partial tuple first" );
50
51
```
@@ -55,7 +56,7 @@ Each variable or temporary is associated to a *drop scope*. When control flow
55
56
leaves a drop scope all variables associated to that scope are dropped in
56
57
reverse order of declaration (for variables) or creation (for temporaries).
57
58
58
- Drop scopes are determined after replacing [ ` for ` ] , [ ` if let ` ] and
59
+ Drop scopes are determined after replacing [ ` for ` ] , [ ` if let ` ] , and
59
60
[ ` while let ` ] expressions with the equivalent expressions using [ ` match ` ] .
60
61
Overloaded operators are not distinguished from built-in operators and [ binding
61
62
modes] are not considered.
@@ -82,17 +83,17 @@ from the inside outwards.
82
83
* The parent of a statement scope is the scope of the block that contains the
83
84
statement.
84
85
* The parent of the expression for a ` match ` guard is the scope of the arm that
85
- it's for.
86
+ the guard is for.
86
87
* The parent of the expression for a given ` match ` arm is that arm's scope.
87
- * The parent of the arm scope is the scope of the match expression that it
88
+ * The parent of the arm scope is the scope of the ` match ` expression that it
88
89
belongs to.
89
90
* The parent of all other scopes is the scope of the immediately enclosing
90
91
expression.
91
92
92
93
### Scopes of function parameters
93
94
94
95
All function parameters are in the scope of the entire function body, so are
95
- dropped last when evaluating the function. The actual function parameter is
96
+ dropped last when evaluating the function. Actual function parameters are
96
97
dropped after any named parameters that are bound to parts of it.
97
98
98
99
``` rust
@@ -119,7 +120,7 @@ patterns_in_parameters(
119
120
120
121
Local variables declared in a ` let ` statement are associated to the scope of
121
122
the block that contains the ` let ` statement. Local variables declared in a
122
- match are associated to the arm scope of the ` match ` arm that they are declared
123
+ ` match ` expression are associated to the arm scope of the ` match ` arm that they are declared
123
124
in.
124
125
125
126
``` rust
@@ -136,7 +137,7 @@ let declared_first = ShowOnDrop("Dropped last in outer scope");
136
137
let declared_last = ShowOnDrop (" Dropped first in outer scope" );
137
138
```
138
139
139
- If multiple patterns are used in the same arm for a match, then an unspecified
140
+ If multiple patterns are used in the same arm for a ` match ` expression , then an unspecified
140
141
pattern will be used to determine the drop order.
141
142
142
143
### Temporary scopes
@@ -251,7 +252,7 @@ let x = &mut 0;
251
252
println! (" {}" , x );
252
253
```
253
254
254
- If a borrow, dereference, field or tuple indexing expression has an extended
255
+ If a borrow, dereference, field, or tuple indexing expression has an extended
255
256
temporary scope then so does its operand. If an indexing expression has an
256
257
extended temporary scope then the indexed expression also has an extended
257
258
temporary scope.
@@ -262,7 +263,7 @@ An *extending pattern* is either
262
263
263
264
* An [ identifier pattern] that binds by reference or mutable reference.
264
265
* A [ struct] [ struct pattern ] , [ tuple] [ tuple pattern ] , [ tuple struct] [ tuple
265
- struct pattern] or [ slice] [ slice pattern ] pattern where at least one of the
266
+ struct pattern] , or [ slice] [ slice pattern ] pattern where at least one of the
266
267
direct subpatterns is a extending pattern.
267
268
268
269
So ` ref x ` , ` V(ref x) ` and ` [ref x, y] ` are all extending patterns, but ` x ` ,
@@ -277,13 +278,13 @@ For a let statement with an initializer, an *extending expression* is an
277
278
expression which is one of the following:
278
279
279
280
* The initializer expression.
280
- * The operand of a extending [ borrow expression] .
281
+ * The operand of an extending [ borrow expression] .
281
282
* The operand(s) of an extending [ array] [ array expression ] , [ cast] [ cast
282
- expression] , [ braced struct] [ struct expression ] or [ tuple] [ tuple expression ]
283
+ expression] , [ braced struct] [ struct expression ] , or [ tuple] [ tuple expression ]
283
284
expression.
284
285
* The final expression of any extending [ block expression] [ block expressions ] .
285
286
286
- So the borrow expressions in ` &mut 0 ` , ` (&1, &mut 2) ` and ` Some { 0: &mut 3 } `
287
+ So the borrow expressions in ` &mut 0 ` , ` (&1, &mut 2) ` , and ` Some { 0: &mut 3 } `
287
288
are all extending expressions, while the borrows in ` &0 + &1 ` and
288
289
` Some(&mut 0) ` are not.
289
290
@@ -315,7 +316,7 @@ Here are some examples where expressions don't have extended temporary scopes:
315
316
# fn temp() {}
316
317
# trait Use { fn use_temp(&self) -> &Self { self } }
317
318
# impl Use for () {}
318
- // The temporary that stores the result of `temp()` lives only lives until the
319
+ // The temporary that stores the result of `temp()` only lives until the
319
320
// end of the let statement in these cases.
320
321
321
322
let x = Some(&temp()); // ERROR
0 commit comments