@@ -73,11 +73,13 @@ the following is invalid as it requires the entire Option<String> to be moved
73
73
into a variable called `op_string` while simultaneously requiring the inner
74
74
String to be moved into a variable called `s`.
75
75
76
+ ```
76
77
let x = Some("s".to_string());
77
78
match x {
78
79
op_string @ Some(s) => ...
79
80
None => ...
80
81
}
82
+ ```
81
83
82
84
See also Error 303.
83
85
"## ,
@@ -88,10 +90,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
88
90
referenced in the pattern guard code. Doing so however would prevent the name
89
91
from being available in the body of the match arm. Consider the following:
90
92
93
+ ```
91
94
match Some("hi".to_string()) {
92
95
Some(s) if s.len() == 0 => // use s.
93
96
...
94
97
}
98
+ ```
95
99
96
100
The variable `s` has type String, and its use in the guard is as a variable of
97
101
type String. The guard code effectively executes in a separate scope to the body
@@ -100,11 +104,13 @@ become unavailable in the body of the arm. Although this example seems
100
104
innocuous, the problem is most clear when considering functions that take their
101
105
argument by value.
102
106
107
+ ```
103
108
match Some("hi".to_string()) {
104
109
Some(s) if { drop(s); false } => (),
105
110
Some(s) => // use s.
106
111
...
107
112
}
113
+ ```
108
114
109
115
The value would be dropped in the guard then become unavailable not only in the
110
116
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -117,6 +123,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
117
123
match was succesful. If the match is irrefutable (when it cannot fail to match),
118
124
use a regular `let`-binding instead. For instance:
119
125
126
+ ```
120
127
struct Irrefutable(i32);
121
128
let irr = Irrefutable(0);
122
129
@@ -129,13 +136,15 @@ if let Irrefutable(x) = irr {
129
136
// Try this instead:
130
137
let Irrefutable(x) = irr;
131
138
foo(x);
139
+ ```
132
140
"## ,
133
141
134
142
E0165 : r##"
135
143
A while-let pattern attempts to match the pattern, and enters the body if the
136
144
match was succesful. If the match is irrefutable (when it cannot fail to match),
137
145
use a regular `let`-binding inside a `loop` instead. For instance:
138
146
147
+ ```
139
148
struct Irrefutable(i32);
140
149
let irr = Irrefutable(0);
141
150
@@ -149,6 +158,7 @@ loop {
149
158
let Irrefutable(x) = irr;
150
159
...
151
160
}
161
+ ```
152
162
"## ,
153
163
154
164
E0297 : r##"
@@ -157,6 +167,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
157
167
loop variable, consider using a `match` or `if let` inside the loop body. For
158
168
instance:
159
169
170
+ ```
160
171
// This fails because `None` is not covered.
161
172
for Some(x) in xs {
162
173
...
@@ -176,6 +187,7 @@ for item in xs {
176
187
...
177
188
}
178
189
}
190
+ ```
179
191
"## ,
180
192
181
193
E0301 : r##"
@@ -185,11 +197,13 @@ on which the match depends in such a way, that the match would not be
185
197
exhaustive. For instance, the following would not match any arm if mutable
186
198
borrows were allowed:
187
199
200
+ ```
188
201
match Some(()) {
189
202
None => { },
190
203
option if option.take().is_none() => { /* impossible, option is `Some` */ },
191
204
Some(_) => { } // When the previous match failed, the option became `None`.
192
205
}
206
+ ```
193
207
"## ,
194
208
195
209
E0302 : r##"
@@ -199,18 +213,21 @@ on which the match depends in such a way, that the match would not be
199
213
exhaustive. For instance, the following would not match any arm if assignments
200
214
were allowed:
201
215
216
+ ```
202
217
match Some(()) {
203
218
None => { },
204
219
option if { option = None; false } { },
205
220
Some(_) => { } // When the previous match failed, the option became `None`.
206
221
}
222
+ ```
207
223
"## ,
208
224
209
225
E0303 : r##"
210
226
In certain cases it is possible for sub-bindings to violate memory safety.
211
227
Updates to the borrow checker in a future version of Rust may remove this
212
228
restriction, but for now patterns must be rewritten without sub-bindings.
213
229
230
+ ```
214
231
// Code like this...
215
232
match Some(5) {
216
233
ref op_num @ Some(num) => ...
@@ -225,6 +242,7 @@ match Some(5) {
225
242
}
226
243
None => ...
227
244
}
245
+ ```
228
246
229
247
See also https://github.com/rust-lang/rust/issues/14587
230
248
"##
0 commit comments