Skip to content

Commit 998cf5b

Browse files
Add backquotes to have better looking rust code
1 parent ce27d02 commit 998cf5b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/librustc/diagnostics.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ the following is invalid as it requires the entire Option<String> to be moved
7373
into a variable called `op_string` while simultaneously requiring the inner
7474
String to be moved into a variable called `s`.
7575
76+
```
7677
let x = Some("s".to_string());
7778
match x {
7879
op_string @ Some(s) => ...
7980
None => ...
8081
}
82+
```
8183
8284
See also Error 303.
8385
"##,
@@ -88,10 +90,12 @@ name is bound by move in a pattern, it should also be moved to wherever it is
8890
referenced in the pattern guard code. Doing so however would prevent the name
8991
from being available in the body of the match arm. Consider the following:
9092
93+
```
9194
match Some("hi".to_string()) {
9295
Some(s) if s.len() == 0 => // use s.
9396
...
9497
}
98+
```
9599
96100
The variable `s` has type String, and its use in the guard is as a variable of
97101
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
100104
innocuous, the problem is most clear when considering functions that take their
101105
argument by value.
102106
107+
```
103108
match Some("hi".to_string()) {
104109
Some(s) if { drop(s); false } => (),
105110
Some(s) => // use s.
106111
...
107112
}
113+
```
108114
109115
The value would be dropped in the guard then become unavailable not only in the
110116
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
117123
match was succesful. If the match is irrefutable (when it cannot fail to match),
118124
use a regular `let`-binding instead. For instance:
119125
126+
```
120127
struct Irrefutable(i32);
121128
let irr = Irrefutable(0);
122129
@@ -129,13 +136,15 @@ if let Irrefutable(x) = irr {
129136
// Try this instead:
130137
let Irrefutable(x) = irr;
131138
foo(x);
139+
```
132140
"##,
133141

134142
E0165: r##"
135143
A while-let pattern attempts to match the pattern, and enters the body if the
136144
match was succesful. If the match is irrefutable (when it cannot fail to match),
137145
use a regular `let`-binding inside a `loop` instead. For instance:
138146
147+
```
139148
struct Irrefutable(i32);
140149
let irr = Irrefutable(0);
141150
@@ -149,6 +158,7 @@ loop {
149158
let Irrefutable(x) = irr;
150159
...
151160
}
161+
```
152162
"##,
153163

154164
E0297: r##"
@@ -157,6 +167,7 @@ that a name will be extracted in all cases. Instead of pattern matching the
157167
loop variable, consider using a `match` or `if let` inside the loop body. For
158168
instance:
159169
170+
```
160171
// This fails because `None` is not covered.
161172
for Some(x) in xs {
162173
...
@@ -176,6 +187,7 @@ for item in xs {
176187
...
177188
}
178189
}
190+
```
179191
"##,
180192

181193
E0301: r##"
@@ -185,11 +197,13 @@ on which the match depends in such a way, that the match would not be
185197
exhaustive. For instance, the following would not match any arm if mutable
186198
borrows were allowed:
187199
200+
```
188201
match Some(()) {
189202
None => { },
190203
option if option.take().is_none() => { /* impossible, option is `Some` */ },
191204
Some(_) => { } // When the previous match failed, the option became `None`.
192205
}
206+
```
193207
"##,
194208

195209
E0302: r##"
@@ -199,18 +213,21 @@ on which the match depends in such a way, that the match would not be
199213
exhaustive. For instance, the following would not match any arm if assignments
200214
were allowed:
201215
216+
```
202217
match Some(()) {
203218
None => { },
204219
option if { option = None; false } { },
205220
Some(_) => { } // When the previous match failed, the option became `None`.
206221
}
222+
```
207223
"##,
208224

209225
E0303: r##"
210226
In certain cases it is possible for sub-bindings to violate memory safety.
211227
Updates to the borrow checker in a future version of Rust may remove this
212228
restriction, but for now patterns must be rewritten without sub-bindings.
213229
230+
```
214231
// Code like this...
215232
match Some(5) {
216233
ref op_num @ Some(num) => ...
@@ -225,6 +242,7 @@ match Some(5) {
225242
}
226243
None => ...
227244
}
245+
```
228246
229247
See also https://github.com/rust-lang/rust/issues/14587
230248
"##

0 commit comments

Comments
 (0)