@@ -170,37 +170,56 @@ By repeating all parts of the example, you can ensure that your example still
170170compiles, while only showing the parts that are relevant to that part of your
171171explanation.
172172
173- Another case where the use of `#` is handy is when you want to ignore
174- error handling. Lets say you want the following,
173+
174+ ## Using `?` in doc tests
175+
176+ When writing an example, it is rarely useful to include a complete error
177+ handling, as it would add significant amounts of boilerplate code. Instead, you
178+ may want the following:
175179
176180```ignore
181+ /// ```
177182/// use std::io;
178183/// let mut input = String::new();
179184/// io::stdin().read_line(&mut input)?;
185+ /// ```
180186```
181187
182- The problem is that ` ? ` returns a ` Result<T, E> ` and test functions
183- don't return anything so this will give a mismatched types error.
188+ The problem is that ` ? ` returns a ` Result<T, E> ` and test functions don't
189+ return anything, so this will give a mismatched types error.
190+
191+ You can get around this limitation by manually adding a ` main ` that returns
192+ ` Result<T, E> ` , because ` Result<T, E> ` implements the ` Termination ` trait:
184193
185194``` ignore
186195/// A doc test using ?
187196///
188197/// ```
189198/// use std::io;
190- /// # fn foo() -> io::Result<()> {
199+ ///
200+ /// fn main() -> io::Result<()> {
201+ /// let mut input = String::new();
202+ /// io::stdin().read_line(&mut input)?;
203+ /// Ok(())
204+ /// }
205+ /// ```
206+ ```
207+
208+ Together with the ` # ` from the section above, you arrive at a solution that
209+ appears to the reader as the initial idea but works with doc tests:
210+
211+ ``` ignore
212+ /// ```
213+ /// use std::io;
214+ /// # fn main() -> io::Result<()> {
191215/// let mut input = String::new();
192216/// io::stdin().read_line(&mut input)?;
193217/// # Ok(())
194218/// # }
195219/// ```
196- # fn foo() {}
197220```
198221
199- You can get around this by wrapping the code in a function. This catches
200- and swallows the ` Result<T, E> ` when running tests on the docs. This
201- pattern appears regularly in the standard library.
202-
203- ### Documenting macros
222+ ## Documenting macros
204223
205224Here’s an example of documenting a macro:
206225
0 commit comments