Skip to content

Commit 2ffcfeb

Browse files
Add compile_fail documentation
1 parent ccc31ce commit 2ffcfeb

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ that your tests are up to date and working.
55

66
The basic idea is this:
77

8-
```rust,ignore
8+
```ignore
99
/// # Examples
1010
///
1111
/// ```
@@ -106,23 +106,23 @@ our source code:
106106
```text
107107
First, we set `x` to five:
108108
109-
```rust
109+
```
110110
let x = 5;
111111
# let y = 6;
112112
# println!("{}", x + y);
113113
```
114114
115115
Next, we set `y` to six:
116116
117-
```rust
117+
```
118118
# let x = 5;
119119
let y = 6;
120120
# println!("{}", x + y);
121121
```
122122
123123
Finally, we print the sum of `x` and `y`:
124124
125-
```rust
125+
```
126126
# let x = 5;
127127
# let y = 6;
128128
println!("{}", x + y);
@@ -136,7 +136,7 @@ explanation.
136136
Another case where the use of `#` is handy is when you want to ignore
137137
error handling. Lets say you want the following,
138138

139-
```rust,ignore
139+
```ignore
140140
/// use std::io;
141141
/// let mut input = String::new();
142142
/// io::stdin().read_line(&mut input)?;
@@ -145,7 +145,7 @@ error handling. Lets say you want the following,
145145
The problem is that `?` returns a `Result<T, E>` and test functions
146146
don't return anything so this will give a mismatched types error.
147147

148-
```rust,ignore
148+
```ignore
149149
/// A doc test using ?
150150
///
151151
/// ```
@@ -179,7 +179,7 @@ Here’s an example of documenting a macro:
179179
/// # }
180180
/// ```
181181
///
182-
/// ```rust,should_panic
182+
/// ```should_panic
183183
/// # #[macro_use] extern crate foo;
184184
/// # fn main() {
185185
/// panic_unless!(true == false, “I’m broken.”);
@@ -224,7 +224,7 @@ only shows the part you care about.
224224
`should_panic` tells `rustdoc` that the code should compile correctly, but
225225
not actually pass as a test.
226226

227-
```rust
227+
```text
228228
/// ```no_run
229229
/// loop {
230230
/// println!("Hello, world");
@@ -233,6 +233,18 @@ not actually pass as a test.
233233
# fn foo() {}
234234
```
235235

236+
`compile_fail` tells `rustdoc` that the compilation should fail. If it
237+
compiles, then the test will fail. However please note that code failing
238+
with the current Rust release may work in a future release, as new features
239+
are added.
240+
241+
```text
242+
/// ```compile_fail
243+
/// let x = 5;
244+
/// x += 2; // shouldn't compile!
245+
/// ```
246+
```
247+
236248
The `no_run` attribute will compile your code, but not run it. This is
237249
important for examples such as "Here's how to retrieve a web page,"
238250
which you would want to ensure compiles, but might be run in a test

0 commit comments

Comments
 (0)