@@ -75,13 +75,11 @@ the following is invalid as it requires the entire Option<String> to be moved
75
75
into a variable called `op_string` while simultaneously requiring the inner
76
76
String to be moved into a variable called `s`.
77
77
78
- ```
79
- let x = Some("s".to_string());
80
- match x {
81
- op_string @ Some(s) => ...
82
- None => ...
83
- }
84
- ```
78
+ let x = Some("s".to_string());
79
+ match x {
80
+ op_string @ Some(s) => ...
81
+ None => ...
82
+ }
85
83
86
84
See also Error 303.
87
85
"## ,
@@ -92,12 +90,10 @@ name is bound by move in a pattern, it should also be moved to wherever it is
92
90
referenced in the pattern guard code. Doing so however would prevent the name
93
91
from being available in the body of the match arm. Consider the following:
94
92
95
- ```
96
- match Some("hi".to_string()) {
97
- Some(s) if s.len() == 0 => // use s.
98
- ...
99
- }
100
- ```
93
+ match Some("hi".to_string()) {
94
+ Some(s) if s.len() == 0 => // use s.
95
+ ...
96
+ }
101
97
102
98
The variable `s` has type String, and its use in the guard is as a variable of
103
99
type String. The guard code effectively executes in a separate scope to the body
@@ -106,13 +102,11 @@ become unavailable in the body of the arm. Although this example seems
106
102
innocuous, the problem is most clear when considering functions that take their
107
103
argument by value.
108
104
109
- ```
110
- match Some("hi".to_string()) {
111
- Some(s) if { drop(s); false } => (),
112
- Some(s) => // use s.
113
- ...
114
- }
115
- ```
105
+ match Some("hi".to_string()) {
106
+ Some(s) if { drop(s); false } => (),
107
+ Some(s) => // use s.
108
+ ...
109
+ }
116
110
117
111
The value would be dropped in the guard then become unavailable not only in the
118
112
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -129,43 +123,37 @@ This limitation may be removed in a future version of Rust.
129
123
130
124
Wrong example:
131
125
132
- ```
133
- struct X { x: (), }
134
-
135
- let x = Some((X { x: () }, X { x: () }));
136
- match x {
137
- Some((y, ref z)) => {},
138
- None => panic!()
139
- }
140
- ```
126
+ struct X { x: (), }
127
+
128
+ let x = Some((X { x: () }, X { x: () }));
129
+ match x {
130
+ Some((y, ref z)) => {},
131
+ None => panic!()
132
+ }
141
133
142
134
You have two solutions:
143
135
1. Bind the pattern's values the same way:
144
136
145
- ```
146
- struct X { x: (), }
147
-
148
- let x = Some((X { x: () }, X { x: () }));
149
- match x {
150
- Some((ref y, ref z)) => {},
151
- // or Some((y, z)) => {}
152
- None => panic!()
153
- }
154
- ```
137
+ struct X { x: (), }
138
+
139
+ let x = Some((X { x: () }, X { x: () }));
140
+ match x {
141
+ Some((ref y, ref z)) => {},
142
+ // or Some((y, z)) => {}
143
+ None => panic!()
144
+ }
155
145
156
146
2. Implement the `Copy` trait for the X structure (however, please
157
147
keep in mind that the first solution should be preferred!):
158
148
159
- ```
160
- #[derive(Clone, Copy)]
161
- struct X { x: (), }
162
-
163
- let x = Some((X { x: () }, X { x: () }));
164
- match x {
165
- Some((y, ref z)) => {},
166
- None => panic!()
167
- }
168
- ```
149
+ #[derive(Clone, Copy)]
150
+ struct X { x: (), }
151
+
152
+ let x = Some((X { x: () }, X { x: () }));
153
+ match x {
154
+ Some((y, ref z)) => {},
155
+ None => panic!()
156
+ }
169
157
"## ,
170
158
171
159
E0015 : r##"
@@ -187,8 +175,8 @@ them yourself.
187
175
You can build a free-standing crate by adding `#![no_std]` to the crate
188
176
attributes:
189
177
190
- #![feature(no_std)]
191
- #![no_std]
178
+ #![feature(no_std)]
179
+ #![no_std]
192
180
193
181
See also https://doc.rust-lang.org/book/no-stdlib.html
194
182
"## ,
@@ -204,11 +192,11 @@ mutex can be declared `static` as well.
204
192
205
193
If you want to match against a `static`, consider using a guard instead:
206
194
207
- static FORTY_TWO: i32 = 42;
208
- match Some(42) {
209
- Some(x) if x == FORTY_TWO => ...
210
- ...
211
- }
195
+ static FORTY_TWO: i32 = 42;
196
+ match Some(42) {
197
+ Some(x) if x == FORTY_TWO => ...
198
+ ...
199
+ }
212
200
"## ,
213
201
214
202
E0161 : r##"
@@ -224,58 +212,54 @@ An if-let pattern attempts to match the pattern, and enters the body if the
224
212
match was succesful. If the match is irrefutable (when it cannot fail to match),
225
213
use a regular `let`-binding instead. For instance:
226
214
227
- ```
228
- struct Irrefutable(i32);
229
- let irr = Irrefutable(0);
230
-
231
- // This fails to compile because the match is irrefutable.
232
- if let Irrefutable(x) = irr {
233
- // This body will always be executed.
215
+ struct Irrefutable(i32);
216
+ let irr = Irrefutable(0);
217
+
218
+ // This fails to compile because the match is irrefutable.
219
+ if let Irrefutable(x) = irr {
220
+ // This body will always be executed.
221
+ foo(x);
222
+ }
223
+
224
+ // Try this instead:
225
+ let Irrefutable(x) = irr;
234
226
foo(x);
235
- }
236
-
237
- // Try this instead:
238
- let Irrefutable(x) = irr;
239
- foo(x);
240
- ```
241
227
"## ,
242
228
243
229
E0165 : r##"
244
230
A while-let pattern attempts to match the pattern, and enters the body if the
245
231
match was succesful. If the match is irrefutable (when it cannot fail to match),
246
232
use a regular `let`-binding inside a `loop` instead. For instance:
247
233
248
- ```
249
- struct Irrefutable(i32);
250
- let irr = Irrefutable(0);
251
-
252
- // This fails to compile because the match is irrefutable.
253
- while let Irrefutable(x) = irr {
254
- ...
255
- }
256
-
257
- // Try this instead:
258
- loop {
259
- let Irrefutable(x) = irr;
260
- ...
261
- }
262
- ```
234
+ struct Irrefutable(i32);
235
+ let irr = Irrefutable(0);
236
+
237
+ // This fails to compile because the match is irrefutable.
238
+ while let Irrefutable(x) = irr {
239
+ ...
240
+ }
241
+
242
+ // Try this instead:
243
+ loop {
244
+ let Irrefutable(x) = irr;
245
+ ...
246
+ }
263
247
"## ,
264
248
265
249
E0170 : r##"
266
250
Enum variants are qualified by default. For example, given this type:
267
251
268
- enum Method {
269
- GET,
270
- POST
271
- }
252
+ enum Method {
253
+ GET,
254
+ POST
255
+ }
272
256
273
257
you would match it using:
274
258
275
- match m {
276
- Method::GET => ...
277
- Method::POST => ...
278
- }
259
+ match m {
260
+ Method::GET => ...
261
+ Method::POST => ...
262
+ }
279
263
280
264
If you don't qualify the names, the code will bind new variables named "GET" and
281
265
"POST" instead. This behavior is likely not what you want, so rustc warns when
@@ -284,8 +268,8 @@ that happens.
284
268
Qualified names are good practice, and most code works well with them. But if
285
269
you prefer them unqualified, you can import the variants into scope:
286
270
287
- use Method::*;
288
- enum Method { GET, POST }
271
+ use Method::*;
272
+ enum Method { GET, POST }
289
273
"## ,
290
274
291
275
E0267 : r##"
@@ -305,7 +289,7 @@ E0296: r##"
305
289
This error indicates that the given recursion limit could not be parsed. Ensure
306
290
that the value provided is a positive integer between quotes, like so:
307
291
308
- #![recursion_limit="1000"]
292
+ #![recursion_limit="1000"]
309
293
"## ,
310
294
311
295
E0297 : r##"
@@ -314,27 +298,25 @@ that a name will be extracted in all cases. Instead of pattern matching the
314
298
loop variable, consider using a `match` or `if let` inside the loop body. For
315
299
instance:
316
300
317
- ```
318
- // This fails because `None` is not covered.
319
- for Some(x) in xs {
320
- ...
321
- }
322
-
323
- // Match inside the loop instead:
324
- for item in xs {
325
- match item {
326
- Some(x) => ...
327
- None => ...
328
- }
329
- }
330
-
331
- // Or use `if let`:
332
- for item in xs {
333
- if let Some(x) = item {
301
+ // This fails because `None` is not covered.
302
+ for Some(x) in xs {
334
303
...
335
304
}
336
- }
337
- ```
305
+
306
+ // Match inside the loop instead:
307
+ for item in xs {
308
+ match item {
309
+ Some(x) => ...
310
+ None => ...
311
+ }
312
+ }
313
+
314
+ // Or use `if let`:
315
+ for item in xs {
316
+ if let Some(x) = item {
317
+ ...
318
+ }
319
+ }
338
320
"## ,
339
321
340
322
E0301 : r##"
@@ -344,13 +326,11 @@ on which the match depends in such a way, that the match would not be
344
326
exhaustive. For instance, the following would not match any arm if mutable
345
327
borrows were allowed:
346
328
347
- ```
348
- match Some(()) {
349
- None => { },
350
- option if option.take().is_none() => { /* impossible, option is `Some` */ },
351
- Some(_) => { } // When the previous match failed, the option became `None`.
352
- }
353
- ```
329
+ match Some(()) {
330
+ None => { },
331
+ option if option.take().is_none() => { /* impossible, option is `Some` */ },
332
+ Some(_) => { } // When the previous match failed, the option became `None`.
333
+ }
354
334
"## ,
355
335
356
336
E0302 : r##"
@@ -360,36 +340,32 @@ on which the match depends in such a way, that the match would not be
360
340
exhaustive. For instance, the following would not match any arm if assignments
361
341
were allowed:
362
342
363
- ```
364
- match Some(()) {
365
- None => { },
366
- option if { option = None; false } { },
367
- Some(_) => { } // When the previous match failed, the option became `None`.
368
- }
369
- ```
343
+ match Some(()) {
344
+ None => { },
345
+ option if { option = None; false } { },
346
+ Some(_) => { } // When the previous match failed, the option became `None`.
347
+ }
370
348
"## ,
371
349
372
350
E0303 : r##"
373
351
In certain cases it is possible for sub-bindings to violate memory safety.
374
352
Updates to the borrow checker in a future version of Rust may remove this
375
353
restriction, but for now patterns must be rewritten without sub-bindings.
376
354
377
- ```
378
- // Code like this...
379
- match Some(5) {
380
- ref op_num @ Some(num) => ...
381
- None => ...
382
- }
383
-
384
- // After.
385
- match Some("hi".to_string()) {
386
- Some(ref s) => {
387
- let op_string_ref = &Some(&s);
388
- ...
355
+ // Code like this...
356
+ match Some(5) {
357
+ ref op_num @ Some(num) => ...
358
+ None => ...
359
+ }
360
+
361
+ // After.
362
+ match Some("hi".to_string()) {
363
+ Some(ref s) => {
364
+ let op_string_ref = &Some(&s);
365
+ ...
366
+ }
367
+ None => ...
389
368
}
390
- None => ...
391
- }
392
- ```
393
369
394
370
The `op_string_ref` binding has type &Option<&String> in both cases.
395
371
0 commit comments