Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 23badff

Browse files
Add documentation for the doctest standalone attribute
1 parent 3147520 commit 23badff

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/doc/rustdoc/src/write-documentation/documentation-tests.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,57 @@ that the code sample should be compiled using the respective edition of Rust.
376376
# fn foo() {}
377377
```
378378

379+
Starting in the 2024 edition[^edition-note], compatible doctests are merged as one before being
380+
run. We combine doctests for performance reasons: the slowest part of doctests is to compile them.
381+
Merging all of them into one file and compiling this new file, then running the doctests is much
382+
faster. Whether doctests are merged or not, they are run in their own process.
383+
384+
An example of time spent when running doctests:
385+
386+
[sysinfo crate](https://crates.io/crates/sysinfo):
387+
388+
```text
389+
wall-time duration: 4.59s
390+
total compile time: 27.067s
391+
total runtime: 3.969s
392+
```
393+
394+
Rust core library:
395+
396+
```text
397+
wall-time duration: 102s
398+
total compile time: 775.204s
399+
total runtime: 15.487s
400+
```
401+
402+
[^edition-note]: This is based on the edition of the whole crate, not the edition of the individual
403+
test case that may be specified in its code attribute.
404+
405+
In some cases, doctests cannot be merged. For example, if you have:
406+
407+
```rust
408+
//! ```
409+
//! let location = std::panic::Location::caller();
410+
//! assert_eq!(location.line(), 4);
411+
//! ```
412+
```
413+
414+
The problem with this code is that, if you change any other doctests, it'll likely break when
415+
runing `rustdoc --test`, making it tricky to maintain.
416+
417+
This is where the `standalone` attribute comes in: it tells `rustdoc` that a doctest
418+
should not be merged with the others. So the previous code should use it:
419+
420+
```rust
421+
//! ```standalone
422+
//! let location = std::panic::Location::caller();
423+
//! assert_eq!(location.line(), 4);
424+
//! ```
425+
```
426+
427+
In this case, it means that the line information will not change if you add/remove other
428+
doctests.
429+
379430
### Custom CSS classes for code blocks
380431

381432
```rust

0 commit comments

Comments
 (0)