Skip to content

Commit d170ff8

Browse files
Add documentation for --output-format=doctest
1 parent 4547fad commit d170ff8

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/doc/rustdoc/src/unstable-features.md

+61
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ use `-o -`.
524524

525525
## `-w`/`--output-format`: output format
526526

527+
### json
528+
527529
`--output-format json` emits documentation in the experimental
528530
[JSON format](https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc_json_types/). `--output-format html` has no effect,
529531
and is also accepted on stable toolchains.
@@ -542,6 +544,65 @@ It can also be used with `--show-coverage`. Take a look at its
542544
[documentation](#--show-coverage-calculate-the-percentage-of-items-with-documentation) for more
543545
information.
544546

547+
### doctest
548+
549+
`--output-format doctest` emits JSON which gives you information about doctests in the provided crate.
550+
551+
You can use this option like this:
552+
553+
```bash
554+
rustdoc -Zunstable-options --output-format=doctest src/lib.rs
555+
```
556+
557+
For this rust code:
558+
559+
```rust
560+
/// ```
561+
/// let x = 12;
562+
/// ```
563+
pub trait Trait {}
564+
```
565+
566+
The generated output will look like this:
567+
568+
```json
569+
{
570+
"format_version": 1,
571+
"doctests": [
572+
{
573+
"file": "foo.rs",
574+
"line": 1,
575+
"doctest_attributes": {
576+
"original": "",
577+
"should_panic": false,
578+
"no_run": false,
579+
"ignore": "None",
580+
"rust": true,
581+
"test_harness": false,
582+
"compile_fail": false,
583+
"standalone_crate": false,
584+
"error_codes": [],
585+
"edition": null,
586+
"added_css_classes": [],
587+
"unknown": []
588+
},
589+
"original_code": "let x = 12;",
590+
"doctest_code": "#![allow(unused)]\nfn main() {\nlet x = 12;\n}",
591+
"name": "foo.rs - Trait (line 1)"
592+
}
593+
]
594+
}
595+
```
596+
597+
* `format_version` gives you the current version of the generated JSON. If we change the output in any way, the number will increase.
598+
* `doctests` contains the list of doctests present in the crate.
599+
* `file` is the file path where the doctest is located.
600+
* `line` is the line where the doctest starts (so where the \`\`\` is located in the current code).
601+
* `doctest_attributes` contains computed information about the attributes used on the doctests. For more information about doctest attributes, take a look [here](write-documentation/documentation-tests.html#attributes).
602+
* `original_code` is the code as written in the source code before rustdoc modifies it.
603+
* `doctest_code` is the code modified by rustdoc that will be run. If there is a syntax error, this field will not be present.
604+
* `name` is the name generated by rustdoc which represents this doctest.
605+
545606
## `--enable-per-target-ignores`: allow `ignore-foo` style filters for doctests
546607

547608
* Tracking issue: [#64245](https://github.com/rust-lang/rust/issues/64245)

0 commit comments

Comments
 (0)