Skip to content

Commit a48224b

Browse files
committed
Add a section for using assertions into doc tests.
See #47945.
1 parent 1e1bfc7 commit a48224b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ let x = 5;
3131

3232
There's some subtlety though! Read on for more details.
3333

34+
## Passing or failing a doctest
35+
36+
Like regular unit tests, regular doctests are considered to "pass"
37+
if they compile and run without panicking.
38+
So if you want to demonstrate that some computation gives a certain result,
39+
the `assert!` family of macros works the same as other Rust code:
40+
41+
```rust
42+
let foo = "foo";
43+
44+
assert_eq!(foo, "foo");
45+
```
46+
47+
This way, if the computation ever returns something different,
48+
the code panics and the doctest fails.
49+
3450
## Pre-processing examples
3551

3652
In the example above, you'll note something strange: there's no `main`

0 commit comments

Comments
 (0)