@@ -321,21 +321,43 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
321321
322322## Attributes
323323
324- Code blocks can be annotated with attributes that help ` rustdoc ` do the right
325- thing when testing your code:
324+ Code blocks can be annotated with attributes that tell ` rustdoc ` how to build and interpret the test.
325+ They follow the triple backtick in the opening line.
326+ As such, they share the place with language strings like ` rust ` or ` text ` .
327+ Multiple attributes can be provided by separating them with commas, spaces or tabs.
328+ You can also write comments which are enclosed in parentheses ` (…) ` .
326329
327- The ` ignore ` attribute tells Rust to ignore your code. This is almost never
328- what you want as it's the most generic. Instead, consider annotating it
329- with ` text ` if it's not code or using ` # ` s to get a working example that
330- only shows the part you care about.
330+ As alluded to in the introduction at the very top,
331+ unless you specify ` rust ` or something that isn't an attribute (except for ` custom ` ),
332+ the code block is assumed to be Rust source code (and is syntax highlighted as such).
333+
334+ You can of course add ` rust ` explicitly (like ` rust,ignore ` ) if the Markdown is also consumed by
335+ other tools (e.g., if it's contained inside of a ` README.md ` that's included via ` include_str ` ).
336+
337+ ### ` ignore `
338+
339+ The ` ignore ` attribute tells ` rustdoc ` to ignore your code. This is useful if you would like to
340+ have Rust syntax highlighting but the snippet is incomplete or pseudocode.
341+ It is customary to add the reason why it should be ignored in a ` (…) ` comment.
331342
332343``` rust
333344/// ```ignore
334345/// fn foo() {
335346/// ```
347+ ///
348+ /// ```ignore (needs extra dependency)
349+ /// use dependency::functionality;
350+ /// functionality();
351+ /// ```
336352# fn foo () {}
337353```
338354
355+ Do note that this is almost never what you want as it's the most generic.
356+ Instead, consider annotating it with ` text ` if it's not code or
357+ using ` # ` s to get a working example that only shows the part you care about.
358+
359+ ### ` should_panic `
360+
339361` should_panic ` tells ` rustdoc ` that the code should compile correctly but
340362panic during execution. If the code doesn't panic, the test will fail.
341363
@@ -346,6 +368,8 @@ panic during execution. If the code doesn't panic, the test will fail.
346368# fn foo () {}
347369```
348370
371+ ### ` no_run `
372+
349373The ` no_run ` attribute will compile your code but not run it. This is
350374important for examples such as "Here's how to retrieve a web page,"
351375which you would want to ensure compiles, but might be run in a test
@@ -361,11 +385,10 @@ used to demonstrate code snippets that can cause Undefined Behavior.
361385# fn foo () {}
362386```
363387
364- ` compile_fail ` tells ` rustdoc ` that the compilation should fail. If it
365- compiles, then the test will fail. However, please note that code failing
366- with the current Rust release may work in a future release, as new features
367- are added.
388+ ### ` compile_fail `
368389
390+ ` compile_fail ` tells ` rustdoc ` that the compilation should fail. If it
391+ compiles, then the test will fail.
369392``` rust
370393/// ```compile_fail
371394/// let x = 5;
@@ -374,6 +397,13 @@ are added.
374397# fn foo () {}
375398```
376399
400+ <div class =" warning " >
401+ However, please note that code failing with the current Rust release may work in a future release,
402+ as new features are added!
403+ </div >
404+
405+ ### ` edition… `
406+
377407` edition2015 ` , ` edition2018 ` , ` edition2021 ` , and ` edition2024 ` tell ` rustdoc `
378408that the code sample should be compiled using the respective edition of Rust.
379409
@@ -390,6 +420,8 @@ that the code sample should be compiled using the respective edition of Rust.
390420# fn foo () {}
391421```
392422
423+ ### ` standalone_crate `
424+
393425Starting in the 2024 edition[ ^ edition-note ] , compatible doctests are merged as one before being
394426run. We combine doctests for performance reasons: the slowest part of doctests is to compile them.
395427Merging all of them into one file and compiling this new file, then running the doctests is much
@@ -441,7 +473,7 @@ should not be merged with the others. So the previous code should use it:
441473In this case, it means that the line information will not change if you add/remove other
442474doctests.
443475
444- ### Ignoring targets
476+ ### ` ignore-… ` : Ignoring targets
445477
446478Attributes starting with ` ignore- ` can be used to ignore doctests for specific
447479targets. For example, ` ignore-x86_64 ` will avoid building doctests when the
@@ -478,7 +510,7 @@ struct Foo;
478510In older versions, this will be ignored on all targets, but starting with
479511version 1.88.0, ` ignore-x86_64 ` will override ` ignore ` .
480512
481- ### Custom CSS classes for code blocks
513+ ### ` {…} ` & ` custom ` : Custom CSS classes for code blocks
482514
483515``` rust
484516/// ```custom,{class=language-c}
@@ -504,8 +536,9 @@ To be noted that you can replace `class=` with `.` to achieve the same result:
504536pub struct Bar ;
505537```
506538
507- To be noted, ` rust ` and ` .rust ` /` class=rust ` have different effects: ` rust ` indicates that this is
508- a Rust code block whereas the two others add a "rust" CSS class on the code block.
539+ To be noted, ` rust ` and ` {.rust} ` / ` {class=rust} ` have different effects:
540+ ` rust ` indicates that this is a Rust code block whereas
541+ the two others add a "rust" CSS class on the code block.
509542
510543You can also use double quotes:
511544
@@ -516,6 +549,27 @@ You can also use double quotes:
516549pub struct Bar ;
517550```
518551
552+ ### ` test_harness `
553+
554+ With ` test_harness ` applied, ` rustdoc ` will run any contained * test functions*
555+ instead of the (potentially implicit) ` main ` function.
556+
557+ ``` rust
558+ // ! ```test_harness
559+ // ! #[test]
560+ // ! #[should_panic]
561+ // ! fn abc() { assert!(false); }
562+ // !
563+ // ! #[test]
564+ // ! fn xyz() { assert!(true); }
565+ // ! ```
566+ ```
567+
568+ You can read more about * test functions* in [ the Book] [ testing-book ] or in [ the Rust Reference] [ testing-ref ] .
569+
570+ [ testing-book ] : ../book/src/doc/book/ch11-01-writing-tests.html
571+ [ testing-ref ] : ../reference/attributes/testing.html
572+
519573## Syntax reference
520574
521575The * exact* syntax for code blocks, including the edge cases, can be found
0 commit comments