11# Documentation tests
22
3- ` rustdoc ` supports executing your documentation examples as tests. This makes sure
4- that examples within your documentation are up to date and working.
3+ ` rustdoc ` supports executing your documentation examples as tests.
4+ This makes sure that examples within your documentation are up to date and working.
55
66The basic idea is this:
77
@@ -14,11 +14,13 @@ The basic idea is this:
1414# fn f() {}
1515```
1616
17- The triple backticks start and end code blocks. If this were in a file named ` foo.rs ` ,
18- running ` rustdoc --test foo.rs ` will extract this example, and then run it as a test.
17+ Here, the triple backticks start and end the code block.
18+ If this were in a file named ` foo.rs ` , running ` rustdoc --test foo.rs ` will extract this example,
19+ and then run it as a test.
1920
20- Please note that by default, if no language is set for the block code, rustdoc
21- assumes it is Rust code. So the following:
21+ Please note that by default,
22+ if no language is set for the block code, rustdoc assumes it is Rust code.
23+ So the following:
2224
2325`````` markdown
2426``` rust
@@ -321,21 +323,43 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
321323
322324## Attributes
323325
324- Code blocks can be annotated with attributes that help ` rustdoc ` do the right
325- thing when testing your code:
326+ Code blocks can be annotated with attributes that tell ` rustdoc ` how to build and interpret the test.
327+ They follow the [ code fence] in the opening line.
328+ As such, they share the place with language strings like ` rust ` or ` text ` .
329+ Multiple attributes can be provided by separating them with commas, spaces or tabs.
330+ You can also write comments which are enclosed in parentheses ` (…) ` .
326331
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.
332+ As alluded to in the introduction at the very top,
333+ unless you specify ` rust ` or something that isn't an attribute (except for ` custom ` ),
334+ the code block is assumed to be Rust source code (and is syntax highlighted as such).
335+
336+ You can of course add ` rust ` explicitly (like ` rust,ignore ` ) if the Markdown is also consumed by
337+ other tools (e.g., if it's contained inside of a ` README.md ` that's included via ` include_str ` ).
338+
339+ ### ` ignore `
340+
341+ The ` ignore ` attribute tells ` rustdoc ` to ignore your code. This is useful if you would like to
342+ have Rust syntax highlighting but the snippet is incomplete or pseudocode.
343+ It is customary to add the reason why it should be ignored in a ` (…) ` comment.
331344
332345``` rust
333346/// ```ignore
334347/// fn foo() {
335348/// ```
349+ ///
350+ /// ```ignore (needs extra dependency)
351+ /// use dependency::functionality;
352+ /// functionality();
353+ /// ```
336354# fn foo () {}
337355```
338356
357+ Do note that this is almost never what you want as it's the most generic.
358+ Instead, consider annotating it with ` text ` if it's not code or
359+ using ` # ` s to get a working example that only shows the part you care about.
360+
361+ ### ` should_panic `
362+
339363` should_panic ` tells ` rustdoc ` that the code should compile correctly but
340364panic during execution. If the code doesn't panic, the test will fail.
341365
@@ -346,6 +370,8 @@ panic during execution. If the code doesn't panic, the test will fail.
346370# fn foo () {}
347371```
348372
373+ ### ` no_run `
374+
349375The ` no_run ` attribute will compile your code but not run it. This is
350376important for examples such as "Here's how to retrieve a web page,"
351377which you would want to ensure compiles, but might be run in a test
@@ -361,10 +387,10 @@ used to demonstrate code snippets that can cause Undefined Behavior.
361387# fn foo () {}
362388```
363389
390+ ### ` compile_fail `
391+
364392` 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.
393+ compiles, then the test will fail.
368394
369395``` rust
370396/// ```compile_fail
@@ -374,6 +400,13 @@ are added.
374400# fn foo () {}
375401```
376402
403+ <div class =" warning " >
404+ However, please note that code failing with the current Rust release may work in a future release,
405+ as new features are added!
406+ </div >
407+
408+ ### ` edition… `
409+
377410` edition2015 ` , ` edition2018 ` , ` edition2021 ` , and ` edition2024 ` tell ` rustdoc `
378411that the code sample should be compiled using the respective edition of Rust.
379412
@@ -390,6 +423,8 @@ that the code sample should be compiled using the respective edition of Rust.
390423# fn foo () {}
391424```
392425
426+ ### ` standalone_crate `
427+
393428Starting in the 2024 edition[ ^ edition-note ] , compatible doctests are merged as one before being
394429run. We combine doctests for performance reasons: the slowest part of doctests is to compile them.
395430Merging all of them into one file and compiling this new file, then running the doctests is much
@@ -441,7 +476,7 @@ should not be merged with the others. So the previous code should use it:
441476In this case, it means that the line information will not change if you add/remove other
442477doctests.
443478
444- ### Ignoring targets
479+ ### ` ignore-… ` : Ignoring targets
445480
446481Attributes starting with ` ignore- ` can be used to ignore doctests for specific
447482targets. For example, ` ignore-x86_64 ` will avoid building doctests when the
@@ -478,7 +513,7 @@ struct Foo;
478513In older versions, this will be ignored on all targets, but starting with
479514version 1.88.0, ` ignore-x86_64 ` will override ` ignore ` .
480515
481- ### Custom CSS classes for code blocks
516+ ### ` {…} ` & ` custom ` : Custom CSS classes for code blocks
482517
483518``` rust
484519/// ```custom,{class=language-c}
@@ -504,8 +539,9 @@ To be noted that you can replace `class=` with `.` to achieve the same result:
504539pub struct Bar ;
505540```
506541
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.
542+ To be noted, ` rust ` and ` {.rust} ` / ` {class=rust} ` have different effects:
543+ ` rust ` indicates that this is a Rust code block whereas
544+ the two others add a "rust" CSS class on the code block in the generated HTML.
509545
510546You can also use double quotes:
511547
@@ -516,24 +552,42 @@ You can also use double quotes:
516552pub struct Bar ;
517553```
518554
555+ ### ` test_harness `
556+
557+ With ` test_harness ` applied, ` rustdoc ` will run any contained * test functions*
558+ instead of the (potentially implicit) ` main ` function.
559+
560+ ``` rust
561+ // ! ```test_harness
562+ // ! #[test]
563+ // ! #[should_panic]
564+ // ! fn abc() { assert!(false); }
565+ // !
566+ // ! #[test]
567+ // ! fn xyz() { assert!(true); }
568+ // ! ```
569+ ```
570+
571+ You can read more about * test functions* in [ the Book] [ testing-book ] or in [ the Rust Reference] [ testing-ref ] .
572+
573+ [ testing-book ] : ../../book/ch11-01-writing-tests.html
574+ [ testing-ref ] : ../../reference/attributes/testing.html
575+
519576## Syntax reference
520577
521- The * exact* syntax for code blocks, including the edge cases, can be found
522- in the [ Fenced Code Blocks] ( https://spec.commonmark.org/0.29/#fenced-code-blocks )
523- section of the CommonMark specification.
578+ The * exact* syntax for code blocks, including the edge cases,
579+ can be found in the [ Fenced Code Blocks] section of the CommonMark specification.
524580
525- Rustdoc also accepts * indented* code blocks as an alternative to fenced
526- code blocks: instead of surrounding your code with three backticks, you
527- can indent each line by four or more spaces.
581+ Rustdoc also accepts * indented* code blocks as an alternative to fenced code blocks:
582+ Instead of surrounding your code with a [ code fence ] (e.g., three backticks),
583+ you can indent each line by four or more spaces.
528584
529585`````` markdown
530586 let foo = "foo";
531587 assert_eq!(foo, "foo");
532588``````
533589
534- These, too, are documented in the CommonMark specification, in the
535- [ Indented Code Blocks] ( https://spec.commonmark.org/0.29/#indented-code-blocks )
536- section.
590+ These, too, are documented in the CommonMark specification, in the [ Indented Code Blocks] section.
537591
538592However, it's preferable to use fenced code blocks over indented code blocks.
539593Not only are fenced code blocks considered more idiomatic for Rust code,
@@ -595,3 +649,8 @@ operations within documentation test examples, such as `std::fs::read_to_string`
595649The ` --test-run-directory ` flag allows controlling the run directory separately from the compilation directory.
596650This is particularly useful in workspaces, where compiler invocations and thus diagnostics should be
597651relative to the workspace directory, but documentation test examples should run relative to the crate directory.
652+
653+
654+ [ code fence ] : https://spec.commonmark.org/0.29/#code-fence
655+ [ Fenced Code Blocks ] : https://spec.commonmark.org/0.29/#fenced-code-blocks
656+ [ Indented Code Blocks ] : https://spec.commonmark.org/0.29/#indented-code-blocks
0 commit comments