@@ -297,20 +297,23 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
297297
298298## Attributes
299299
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
301301thing when testing your code:
302302
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+
303308``` rust
304309/// ```ignore
305310/// fn foo() {
306311/// ```
307312# fn foo () {}
308313```
309314
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.
314317
315318``` rust
316319/// ```should_panic
@@ -319,8 +322,10 @@ only shows the part you care about.
319322# fn foo () {}
320323```
321324
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.
324329
325330``` rust
326331/// ```no_run
@@ -331,24 +336,24 @@ not actually pass as a test.
331336# fn foo () {}
332337```
333338
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 .
338343
339- ``` text
344+ ``` rust
340345/// ```compile_fail
341346/// let x = 5;
342347/// x += 2; // shouldn't compile!
343348/// ```
349+ # fn foo () {}
344350```
345351
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.
350355
351- ``` text
356+ ``` rust
352357/// Only runs on the 2018 edition.
353358///
354359/// ```edition2018
@@ -358,12 +363,9 @@ are added.
358363/// + "3".parse::<i32>()?
359364/// };
360365/// ```
366+ # fn foo () {}
361367```
362368
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-
367369## Syntax reference
368370
369371The * exact* syntax for code blocks, including the edge cases, can be found
@@ -385,7 +387,7 @@ section.
385387
386388However, it's preferable to use fenced code blocks over indented code blocks.
387389Not 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
389391indented code blocks.
390392
391393### Include items only when collecting doctests
0 commit comments