@@ -75,11 +75,13 @@ 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
- let x = Some("s".to_string());
79
- match x {
80
- op_string @ Some(s) => ...
81
- None => ...
82
- }
78
+ ```
79
+ let x = Some("s".to_string());
80
+ match x {
81
+ op_string @ Some(s) => ...
82
+ None => ...
83
+ }
84
+ ```
83
85
84
86
See also Error 303.
85
87
"## ,
@@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
90
92
referenced in the pattern guard code. Doing so however would prevent the name
91
93
from being available in the body of the match arm. Consider the following:
92
94
93
- match Some("hi".to_string()) {
94
- Some(s) if s.len() == 0 => // use s.
95
- ...
96
- }
95
+ ```
96
+ match Some("hi".to_string()) {
97
+ Some(s) if s.len() == 0 => // use s.
98
+ ...
99
+ }
100
+ ```
97
101
98
102
The variable `s` has type String, and its use in the guard is as a variable of
99
103
type String. The guard code effectively executes in a separate scope to the body
@@ -102,11 +106,13 @@ become unavailable in the body of the arm. Although this example seems
102
106
innocuous, the problem is most clear when considering functions that take their
103
107
argument by value.
104
108
105
- match Some("hi".to_string()) {
106
- Some(s) if { drop(s); false } => (),
107
- Some(s) => // use s.
108
- ...
109
- }
109
+ ```
110
+ match Some("hi".to_string()) {
111
+ Some(s) if { drop(s); false } => (),
112
+ Some(s) => // use s.
113
+ ...
114
+ }
115
+ ```
110
116
111
117
The value would be dropped in the guard then become unavailable not only in the
112
118
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -123,37 +129,43 @@ This limitation may be removed in a future version of Rust.
123
129
124
130
Wrong example:
125
131
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
- }
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
+ ```
133
141
134
142
You have two solutions:
135
143
1. Bind the pattern's values the same way:
136
144
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
- }
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
+ ```
145
155
146
156
2. Implement the `Copy` trait for the X structure (however, please
147
157
keep in mind that the first solution should be preferred!):
148
158
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
- }
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
+ ```
157
169
"## ,
158
170
159
171
E0015 : r##"
@@ -175,8 +187,10 @@ them yourself.
175
187
You can build a free-standing crate by adding `#![no_std]` to the crate
176
188
attributes:
177
189
178
- #![feature(no_std)]
179
- #![no_std]
190
+ ```
191
+ #![feature(no_std)]
192
+ #![no_std]
193
+ ```
180
194
181
195
See also https://doc.rust-lang.org/book/no-stdlib.html
182
196
"## ,
@@ -192,11 +206,13 @@ mutex can be declared `static` as well.
192
206
193
207
If you want to match against a `static`, consider using a guard instead:
194
208
195
- static FORTY_TWO: i32 = 42;
196
- match Some(42) {
197
- Some(x) if x == FORTY_TWO => ...
198
- ...
199
- }
209
+ ```
210
+ static FORTY_TWO: i32 = 42;
211
+ match Some(42) {
212
+ Some(x) if x == FORTY_TWO => ...
213
+ ...
214
+ }
215
+ ```
200
216
"## ,
201
217
202
218
E0161 : r##"
@@ -212,54 +228,60 @@ An if-let pattern attempts to match the pattern, and enters the body if the
212
228
match was succesful. If the match is irrefutable (when it cannot fail to match),
213
229
use a regular `let`-binding instead. For instance:
214
230
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;
231
+ ```
232
+ struct Irrefutable(i32);
233
+ let irr = Irrefutable(0);
234
+
235
+ // This fails to compile because the match is irrefutable.
236
+ if let Irrefutable(x) = irr {
237
+ // This body will always be executed.
226
238
foo(x);
239
+ }
240
+
241
+ // Try this instead:
242
+ let Irrefutable(x) = irr;
243
+ foo(x);
244
+ ```
227
245
"## ,
228
246
229
247
E0165 : r##"
230
248
A while-let pattern attempts to match the pattern, and enters the body if the
231
249
match was succesful. If the match is irrefutable (when it cannot fail to match),
232
250
use a regular `let`-binding inside a `loop` instead. For instance:
233
251
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
- }
252
+ ```
253
+ struct Irrefutable(i32);
254
+ let irr = Irrefutable(0);
255
+
256
+ // This fails to compile because the match is irrefutable.
257
+ while let Irrefutable(x) = irr {
258
+ ...
259
+ }
260
+
261
+ // Try this instead:
262
+ loop {
263
+ let Irrefutable(x) = irr;
264
+ ...
265
+ }
266
+ ```
247
267
"## ,
248
268
249
269
E0170 : r##"
250
270
Enum variants are qualified by default. For example, given this type:
251
271
252
- enum Method {
253
- GET,
254
- POST
255
- }
272
+ ```
273
+ enum Method {
274
+ GET,
275
+ POST
276
+ }
277
+ ```
256
278
257
279
you would match it using:
258
280
259
- match m {
260
- Method::GET => ...
261
- Method::POST => ...
262
- }
281
+ match m {
282
+ Method::GET => ...
283
+ Method::POST => ...
284
+ }
263
285
264
286
If you don't qualify the names, the code will bind new variables named "GET" and
265
287
"POST" instead. This behavior is likely not what you want, so rustc warns when
0 commit comments