Skip to content

Commit 97b489e

Browse files
authored
Rollup merge of rust-lang#48961 - Songbird0:rustdoc_doctests_assertions, r=GuillaumeGomez
Rustdoc: example of use of assertions I added this section at the beginning of the file because it seems to be basic information. Let me know if there's someplace more relevant. See rust-lang#47945.
2 parents 292c6ca + a48224b commit 97b489e

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
@@ -35,6 +35,22 @@ let x = 5;
3535

3636
There's some subtlety though! Read on for more details.
3737

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

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

0 commit comments

Comments
 (0)