@@ -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,10 +385,10 @@ used to demonstrate code snippets that can cause Undefined Behavior.
361385# fn foo () {}
362386```
363387
388+ ### ` compile_fail `
389+
364390` 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.
391+ compiles, then the test will fail.
368392
369393``` rust
370394/// ```compile_fail
@@ -374,6 +398,13 @@ are added.
374398# fn foo () {}
375399```
376400
401+ <div class =" warning " >
402+ However, please note that code failing with the current Rust release may work in a future release,
403+ as new features are added!
404+ </div >
405+
406+ ### ` edition… `
407+
377408` edition2015 ` , ` edition2018 ` , ` edition2021 ` , and ` edition2024 ` tell ` rustdoc `
378409that the code sample should be compiled using the respective edition of Rust.
379410
@@ -390,6 +421,8 @@ that the code sample should be compiled using the respective edition of Rust.
390421# fn foo () {}
391422```
392423
424+ ### ` standalone_crate `
425+
393426Starting in the 2024 edition[ ^ edition-note ] , compatible doctests are merged as one before being
394427run. We combine doctests for performance reasons: the slowest part of doctests is to compile them.
395428Merging all of them into one file and compiling this new file, then running the doctests is much
@@ -441,7 +474,7 @@ should not be merged with the others. So the previous code should use it:
441474In this case, it means that the line information will not change if you add/remove other
442475doctests.
443476
444- ### Ignoring targets
477+ ### ` ignore-… ` : Ignoring targets
445478
446479Attributes starting with ` ignore- ` can be used to ignore doctests for specific
447480targets. For example, ` ignore-x86_64 ` will avoid building doctests when the
@@ -478,7 +511,7 @@ struct Foo;
478511In older versions, this will be ignored on all targets, but starting with
479512version 1.88.0, ` ignore-x86_64 ` will override ` ignore ` .
480513
481- ### Custom CSS classes for code blocks
514+ ### ` {…} ` & ` custom ` : Custom CSS classes for code blocks
482515
483516``` rust
484517/// ```custom,{class=language-c}
@@ -504,8 +537,9 @@ To be noted that you can replace `class=` with `.` to achieve the same result:
504537pub struct Bar ;
505538```
506539
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.
540+ To be noted, ` rust ` and ` {.rust} ` / ` {class=rust} ` have different effects:
541+ ` rust ` indicates that this is a Rust code block whereas
542+ the two others add a "rust" CSS class on the code block.
509543
510544You can also use double quotes:
511545
@@ -516,6 +550,27 @@ You can also use double quotes:
516550pub struct Bar ;
517551```
518552
553+ ### ` test_harness `
554+
555+ With ` test_harness ` applied, ` rustdoc ` will run any contained * test functions*
556+ instead of the (potentially implicit) ` main ` function.
557+
558+ ``` rust
559+ // ! ```test_harness
560+ // ! #[test]
561+ // ! #[should_panic]
562+ // ! fn abc() { assert!(false); }
563+ // !
564+ // ! #[test]
565+ // ! fn xyz() { assert!(true); }
566+ // ! ```
567+ ```
568+
569+ You can read more about * test functions* in [ the Book] [ testing-book ] or in [ the Rust Reference] [ testing-ref ] .
570+
571+ [ testing-book ] : ../../book/book/ch11-01-writing-tests.html
572+ [ testing-ref ] : ../../reference/attributes/testing.html
573+
519574## Syntax reference
520575
521576The * exact* syntax for code blocks, including the edge cases, can be found
0 commit comments