@@ -5,7 +5,7 @@ that your tests are up to date and working.
55
66The basic idea is this:
77
8- ``` rust, ignore
8+ ``` ignore
99/// # Examples
1010///
1111/// ```
@@ -16,6 +16,19 @@ The basic idea is this:
1616The triple backticks start and end code blocks. If this were in a file named ` foo.rs ` ,
1717running ` rustdoc --test foo.rs ` will extract this example, and then run it as a test.
1818
19+ Please note that by default, if no language is set for the block code, ` rustdoc `
20+ assumes it is ` Rust ` code. So the following:
21+
22+ ``` rust
23+ let x = 5 ;
24+ ```
25+
26+ is strictly equivalent to:
27+
28+ ```
29+ let x = 5;
30+ ```
31+
1932There's some subtlety though! Read on for more details.
2033
2134## Pre-processing examples
@@ -106,23 +119,23 @@ our source code:
106119```text
107120 First , we set `x ` to five :
108121
109- ```rust
122+ ```
110123 let x = 5 ;
111124 # let y = 6 ;
112125 # println! (" {}" , x + y );
113126 ```
114127
115128 Next , we set `y ` to six :
116129
117- ```rust
130+ ```
118131 # let x = 5 ;
119132 let y = 6 ;
120133 # println! (" {}" , x + y );
121134 ```
122135
123136 Finally , we print the sum of `x ` and `y `:
124137
125- ```rust
138+ ```
126139 # let x = 5 ;
127140 # let y = 6 ;
128141 println! (" {}" , x + y );
@@ -136,7 +149,7 @@ explanation.
136149Another case where the use of ` # ` is handy is when you want to ignore
137150error handling. Lets say you want the following,
138151
139- ``` rust, ignore
152+ ``` ignore
140153/// use std::io;
141154/// let mut input = String::new();
142155/// io::stdin().read_line(&mut input)?;
@@ -145,7 +158,7 @@ error handling. Lets say you want the following,
145158The problem is that ` ? ` returns a ` Result<T, E> ` and test functions
146159don't return anything so this will give a mismatched types error.
147160
148- ``` rust, ignore
161+ ``` ignore
149162/// A doc test using ?
150163///
151164/// ```
@@ -179,7 +192,7 @@ Here’s an example of documenting a macro:
179192/// # }
180193/// ```
181194///
182- /// ```rust, should_panic
195+ /// ```should_panic
183196/// # #[macro_use] extern crate foo;
184197/// # fn main() {
185198/// panic_unless!(true == false, “I’m broken.”);
@@ -224,7 +237,7 @@ only shows the part you care about.
224237` should_panic ` tells ` rustdoc ` that the code should compile correctly, but
225238not actually pass as a test.
226239
227- ``` rust
240+ ``` text
228241/// ```no_run
229242/// loop {
230243/// println!("Hello, world");
@@ -233,6 +246,18 @@ not actually pass as a test.
233246# fn foo() {}
234247```
235248
249+ ` compile_fail ` tells ` rustdoc ` that the compilation should fail. If it
250+ compiles, then the test will fail. However please note that code failing
251+ with the current Rust release may work in a future release, as new features
252+ are added.
253+
254+ ``` text
255+ /// ```compile_fail
256+ /// let x = 5;
257+ /// x += 2; // shouldn't compile!
258+ /// ```
259+ ```
260+
236261The ` no_run ` attribute will compile your code, but not run it. This is
237262important for examples such as "Here's how to retrieve a web page,"
238263which you would want to ensure compiles, but might be run in a test
0 commit comments