@@ -25,29 +25,29 @@ pointer, [`std::ptr::drop_in_place`] can be used.
25
25
Some examples:
26
26
27
27
``` rust
28
- struct ShowOnDrop (& 'static str );
28
+ struct PrintOnDrop (& 'static str );
29
29
30
- impl Drop for ShowOnDrop {
30
+ impl Drop for PrintOnDrop {
31
31
fn drop (& mut self ) {
32
32
println! (" {}" , self . 0 );
33
33
}
34
34
}
35
35
36
- let mut overwritten = ShowOnDrop (" drops when overwritten" );
37
- overwritten = ShowOnDrop (" drops when scope ends" );
36
+ let mut overwritten = PrintOnDrop (" drops when overwritten" );
37
+ overwritten = PrintOnDrop (" drops when scope ends" );
38
38
39
- let tuple = (ShowOnDrop (" Tuple first" ), ShowOnDrop (" Tuple second" ));
39
+ let tuple = (PrintOnDrop (" Tuple first" ), PrintOnDrop (" Tuple second" ));
40
40
41
41
let moved ;
42
42
// No destructor run on assignment.
43
- moved = ShowOnDrop (" Drops when moved" );
43
+ moved = PrintOnDrop (" Drops when moved" );
44
44
// Drops now, but is then uninitialized.
45
45
moved ;
46
46
47
- let uninitialized : ShowOnDrop ;
47
+ let uninitialized : PrintOnDrop ;
48
48
// Only the first element is dropped.
49
- let mut partially_initialized : (ShowOnDrop , ShowOnDrop );
50
- partially_initialized . 0 = ShowOnDrop (" Partial tuple first" );
49
+ let mut partially_initialized : (PrintOnDrop , PrintOnDrop );
50
+ partially_initialized . 0 = PrintOnDrop (" Partial tuple first" );
51
51
```
52
52
53
53
## Drop scopes
@@ -97,22 +97,22 @@ dropped last when evaluating the function. Actual function parameters are
97
97
dropped after any named parameters that are bound to parts of it.
98
98
99
99
``` rust
100
- # struct ShowOnDrop (& 'static str );
101
- # impl Drop for ShowOnDrop {
100
+ # struct PrintOnDrop (& 'static str );
101
+ # impl Drop for PrintOnDrop {
102
102
# fn drop (& mut self ) {
103
103
# println! (" drop({})" , self . 0 );
104
104
# }
105
105
# }
106
106
// Drops the second parameter, then `y`, then the first parameter, then `x`
107
107
fn patterns_in_parameters (
108
- (x , _ ): (ShowOnDrop , ShowOnDrop ),
109
- (_ , y ): (ShowOnDrop , ShowOnDrop ),
108
+ (x , _ ): (PrintOnDrop , PrintOnDrop ),
109
+ (_ , y ): (PrintOnDrop , PrintOnDrop ),
110
110
) {}
111
111
112
112
// drop order is 3 2 0 1
113
113
patterns_in_parameters (
114
- (ShowOnDrop (" 0" ), ShowOnDrop (" 1" )),
115
- (ShowOnDrop (" 2" ), ShowOnDrop (" 3" )),
114
+ (PrintOnDrop (" 0" ), PrintOnDrop (" 1" )),
115
+ (PrintOnDrop (" 2" ), PrintOnDrop (" 3" )),
116
116
);
117
117
```
118
118
@@ -124,17 +124,17 @@ the block that contains the `let` statement. Local variables declared in a
124
124
in.
125
125
126
126
``` rust
127
- # struct ShowOnDrop (& 'static str );
128
- # impl Drop for ShowOnDrop {
127
+ # struct PrintOnDrop (& 'static str );
128
+ # impl Drop for PrintOnDrop {
129
129
# fn drop (& mut self ) {
130
130
# println! (" drop({})" , self . 0 );
131
131
# }
132
132
# }
133
- let declared_first = ShowOnDrop (" Dropped last in outer scope" );
133
+ let declared_first = PrintOnDrop (" Dropped last in outer scope" );
134
134
{
135
- let declared_in_block = ShowOnDrop (" Dropped in inner scope" );
135
+ let declared_in_block = PrintOnDrop (" Dropped in inner scope" );
136
136
}
137
- let declared_last = ShowOnDrop (" Dropped first in outer scope" );
137
+ let declared_last = PrintOnDrop (" Dropped first in outer scope" );
138
138
```
139
139
140
140
If multiple patterns are used in the same arm for a ` match ` expression, then an unspecified
@@ -153,7 +153,8 @@ smallest scope that contains the expression and is for one of the following:
153
153
* A statement.
154
154
* The body of a [ ` if ` ] , [ ` while ` ] or [ ` loop ` ] expression.
155
155
* The ` else ` block of an ` if ` expression.
156
- * The condition expression of an ` if ` or ` while ` expression, or a ` match ` guard.
156
+ * The condition expression of an ` if ` or ` while ` expression, or a ` match `
157
+ guard.
157
158
* The expression for a match arm.
158
159
* The second operand of a [ lazy boolean expression] .
159
160
@@ -164,36 +165,36 @@ smallest scope that contains the expression and is for one of the following:
164
165
Some examples:
165
166
166
167
``` rust
167
- # struct ShowOnDrop (& 'static str );
168
- # impl Drop for ShowOnDrop {
168
+ # struct PrintOnDrop (& 'static str );
169
+ # impl Drop for PrintOnDrop {
169
170
# fn drop (& mut self ) {
170
171
# println! (" drop({})" , self . 0 );
171
172
# }
172
173
# }
173
- let local_var = ShowOnDrop (" local var" );
174
+ let local_var = PrintOnDrop (" local var" );
174
175
175
176
// Dropped once the condition has been evaluated
176
- if ShowOnDrop (" If condition" ). 0 == " If condition" {
177
+ if PrintOnDrop (" If condition" ). 0 == " If condition" {
177
178
// Dropped at the end of the block
178
- ShowOnDrop (" If body" ). 0
179
+ PrintOnDrop (" If body" ). 0
179
180
} else {
180
181
unreachable! ()
181
182
};
182
183
183
184
// Dropped at the end of the statement
184
- (ShowOnDrop (" first operand" ). 0 == ""
185
+ (PrintOnDrop (" first operand" ). 0 == ""
185
186
// Dropped at the )
186
- || ShowOnDrop (" second operand" ). 0 == "" )
187
+ || PrintOnDrop (" second operand" ). 0 == "" )
187
188
// Dropped at the end of the expression
188
- || ShowOnDrop (" third operand" ). 0 == "" ;
189
+ || PrintOnDrop (" third operand" ). 0 == "" ;
189
190
190
191
// Dropped at the end of the function, after local variables.
191
192
// Changing this to a statement containing a return expression would make the
192
193
// temporary be dropped before the local variables. Binding to a variable
193
194
// which is then returned would also make the temporary be dropped first.
194
- match ShowOnDrop (" Matched value in final expression" ) {
195
+ match PrintOnDrop (" Matched value in final expression" ) {
195
196
// Dropped once the condition has been evaluated
196
- _ if ShowOnDrop (" guard condition" ). 0 == "" => (),
197
+ _ if PrintOnDrop (" guard condition" ). 0 == "" => (),
197
198
_ => (),
198
199
}
199
200
```
@@ -207,23 +208,23 @@ once the expression is evaluated, dropping them has no effect unless one of the
207
208
operands to an expression breaks out of the expression, returns, or panics.
208
209
209
210
``` rust
210
- # struct ShowOnDrop (& 'static str );
211
- # impl Drop for ShowOnDrop {
211
+ # struct PrintOnDrop (& 'static str );
212
+ # impl Drop for PrintOnDrop {
212
213
# fn drop (& mut self ) {
213
214
# println! (" drop({})" , self . 0 );
214
215
# }
215
216
# }
216
217
loop {
217
218
// Tuple expression doesn't finish evaluating so operands drop in reverse order
218
219
(
219
- ShowOnDrop (" Outer tuple first" ),
220
- ShowOnDrop (" Outer tuple second" ),
220
+ PrintOnDrop (" Outer tuple first" ),
221
+ PrintOnDrop (" Outer tuple second" ),
221
222
(
222
- ShowOnDrop (" Inner tuple first" ),
223
- ShowOnDrop (" Inner tuple second" ),
223
+ PrintOnDrop (" Inner tuple first" ),
224
+ PrintOnDrop (" Inner tuple second" ),
224
225
break ,
225
226
),
226
- ShowOnDrop (" Never created" ),
227
+ PrintOnDrop (" Never created" ),
227
228
);
228
229
}
229
230
```
0 commit comments