@@ -297,20 +297,23 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
297
297
298
298
## Attributes
299
299
300
- There are a few annotations that are useful to help ` rustdoc ` do the right
300
+ Code blocks can be annotated with attributes that help ` rustdoc ` do the right
301
301
thing when testing your code:
302
302
303
+ The ` ignore ` attribute tells Rust to ignore your code. This is almost never
304
+ what you want as it's the most generic. Instead, consider annotating it
305
+ with ` text ` if it's not code or using ` # ` s to get a working example that
306
+ only shows the part you care about.
307
+
303
308
``` rust
304
309
/// ```ignore
305
310
/// fn foo() {
306
311
/// ```
307
312
# fn foo () {}
308
313
```
309
314
310
- The ` ignore ` directive tells Rust to ignore your code. This is almost never
311
- what you want, as it's the most generic. Instead, consider annotating it
312
- with ` text ` if it's not code, or using ` # ` s to get a working example that
313
- only shows the part you care about.
315
+ ` should_panic ` tells ` rustdoc ` that the code should compile correctly but
316
+ panic during execution. If the code doesn't panic, the test will fail.
314
317
315
318
``` rust
316
319
/// ```should_panic
@@ -319,8 +322,10 @@ only shows the part you care about.
319
322
# fn foo () {}
320
323
```
321
324
322
- ` should_panic ` tells ` rustdoc ` that the code should compile correctly, but
323
- not actually pass as a test.
325
+ The ` no_run ` attribute will compile your code but not run it. This is
326
+ important for examples such as "Here's how to retrieve a web page,"
327
+ which you would want to ensure compiles, but might be run in a test
328
+ environment that has no network access.
324
329
325
330
``` rust
326
331
/// ```no_run
@@ -331,24 +336,24 @@ not actually pass as a test.
331
336
# fn foo () {}
332
337
```
333
338
334
- The ` no_run ` attribute will compile your code, but not run it. This is
335
- important for examples such as "Here's how to retrieve a web page,"
336
- which you would want to ensure compiles, but might be run in a test
337
- environment that has no network access .
339
+ ` compile_fail ` tells ` rustdoc ` that the compilation should fail. If it
340
+ compiles, then the test will fail. However, please note that code failing
341
+ with the current Rust release may work in a future release, as new features
342
+ are added .
338
343
339
- ``` text
344
+ ``` rust
340
345
/// ```compile_fail
341
346
/// let x = 5;
342
347
/// x += 2; // shouldn't compile!
343
348
/// ```
349
+ # fn foo () {}
344
350
```
345
351
346
- ` compile_fail ` tells ` rustdoc ` that the compilation should fail. If it
347
- compiles, then the test will fail. However please note that code failing
348
- with the current Rust release may work in a future release, as new features
349
- are added.
352
+ ` edition2018 ` tells ` rustdoc ` that the code sample should be compiled using
353
+ the 2018 edition of Rust. Similarly, you can specify ` edition2015 ` to compile
354
+ the code with the 2015 edition.
350
355
351
- ``` text
356
+ ``` rust
352
357
/// Only runs on the 2018 edition.
353
358
///
354
359
/// ```edition2018
@@ -358,12 +363,9 @@ are added.
358
363
/// + "3".parse::<i32>()?
359
364
/// };
360
365
/// ```
366
+ # fn foo () {}
361
367
```
362
368
363
- ` edition2018 ` tells ` rustdoc ` that the code sample should be compiled using
364
- the 2018 edition of Rust. Similarly, you can specify ` edition2015 ` to compile
365
- the code with the 2015 edition.
366
-
367
369
## Syntax reference
368
370
369
371
The * exact* syntax for code blocks, including the edge cases, can be found
@@ -385,7 +387,7 @@ section.
385
387
386
388
However, it's preferable to use fenced code blocks over indented code blocks.
387
389
Not only are fenced code blocks considered more idiomatic for Rust code,
388
- but there is no way to use directives such as ` ignore ` or ` should_panic ` with
390
+ but there is no way to use attributes such as ` ignore ` or ` should_panic ` with
389
391
indented code blocks.
390
392
391
393
### Include items only when collecting doctests
0 commit comments