Skip to content

Commit 1d7d019

Browse files
Add backquotes to have better looking rust code
1 parent 49a94f2 commit 1d7d019

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/librustc/diagnostics.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ the following is invalid as it requires the entire Option<String> to be moved
7575
into a variable called `op_string` while simultaneously requiring the inner
7676
String to be moved into a variable called `s`.
7777
78+
```
7879
let x = Some("s".to_string());
7980
match x {
8081
op_string @ Some(s) => ...
8182
None => ...
8283
}
84+
```
8385
8486
See also Error 303.
8587
"##,
@@ -90,10 +92,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
9092
referenced in the pattern guard code. Doing so however would prevent the name
9193
from being available in the body of the match arm. Consider the following:
9294
95+
```
9396
match Some("hi".to_string()) {
9497
Some(s) if s.len() == 0 => // use s.
9598
...
9699
}
100+
```
97101
98102
The variable `s` has type String, and its use in the guard is as a variable of
99103
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
102106
innocuous, the problem is most clear when considering functions that take their
103107
argument by value.
104108
109+
```
105110
match Some("hi".to_string()) {
106111
Some(s) if { drop(s); false } => (),
107112
Some(s) => // use s.
108113
...
109114
}
115+
```
110116
111117
The value would be dropped in the guard then become unavailable not only in the
112118
body of that arm but also in all subsequent arms! The solution is to bind by
@@ -218,6 +224,7 @@ An if-let pattern attempts to match the pattern, and enters the body if the
218224
match was succesful. If the match is irrefutable (when it cannot fail to match),
219225
use a regular `let`-binding instead. For instance:
220226
227+
```
221228
struct Irrefutable(i32);
222229
let irr = Irrefutable(0);
223230
@@ -230,13 +237,15 @@ if let Irrefutable(x) = irr {
230237
// Try this instead:
231238
let Irrefutable(x) = irr;
232239
foo(x);
240+
```
233241
"##,
234242

235243
E0165: r##"
236244
A while-let pattern attempts to match the pattern, and enters the body if the
237245
match was succesful. If the match is irrefutable (when it cannot fail to match),
238246
use a regular `let`-binding inside a `loop` instead. For instance:
239247
248+
```
240249
struct Irrefutable(i32);
241250
let irr = Irrefutable(0);
242251
@@ -250,6 +259,7 @@ loop {
250259
let Irrefutable(x) = irr;
251260
...
252261
}
262+
```
253263
"##,
254264

255265
E0170: r##"
@@ -304,6 +314,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
304314
loop variable, consider using a `match` or `if let` inside the loop body. For
305315
instance:
306316
317+
```
307318
// This fails because `None` is not covered.
308319
for Some(x) in xs {
309320
...
@@ -323,6 +334,7 @@ for item in xs {
323334
...
324335
}
325336
}
337+
```
326338
"##,
327339

328340
E0301: r##"
@@ -332,11 +344,13 @@ on which the match depends in such a way, that the match would not be
332344
exhaustive. For instance, the following would not match any arm if mutable
333345
borrows were allowed:
334346
347+
```
335348
match Some(()) {
336349
None => { },
337350
option if option.take().is_none() => { /* impossible, option is `Some` */ },
338351
Some(_) => { } // When the previous match failed, the option became `None`.
339352
}
353+
```
340354
"##,
341355

342356
E0302: r##"
@@ -346,21 +360,24 @@ on which the match depends in such a way, that the match would not be
346360
exhaustive. For instance, the following would not match any arm if assignments
347361
were allowed:
348362
363+
```
349364
match Some(()) {
350365
None => { },
351366
option if { option = None; false } { },
352367
Some(_) => { } // When the previous match failed, the option became `None`.
353368
}
369+
```
354370
"##,
355371

356372
E0303: r##"
357373
In certain cases it is possible for sub-bindings to violate memory safety.
358374
Updates to the borrow checker in a future version of Rust may remove this
359375
restriction, but for now patterns must be rewritten without sub-bindings.
360376
361-
// Before.
362-
match Some("hi".to_string()) {
363-
ref op_string_ref @ Some(ref s) => ...
377+
```
378+
// Code like this...
379+
match Some(5) {
380+
ref op_num @ Some(num) => ...
364381
None => ...
365382
}
366383
@@ -372,6 +389,7 @@ match Some("hi".to_string()) {
372389
}
373390
None => ...
374391
}
392+
```
375393
376394
The `op_string_ref` binding has type &Option<&String> in both cases.
377395

0 commit comments

Comments
 (0)