Update rustdoc book to suggest using Termination trait instead of hidden ‘foo’ function
This commit is contained in:
parent
f33db06e1d
commit
63885f7f72
1 changed files with 29 additions and 11 deletions
|
@ -168,37 +168,55 @@ By repeating all parts of the example, you can ensure that your example still
|
||||||
compiles, while only showing the parts that are relevant to that part of your
|
compiles, while only showing the parts that are relevant to that part of your
|
||||||
explanation.
|
explanation.
|
||||||
|
|
||||||
Another case where the use of `#` is handy is when you want to ignore
|
|
||||||
error handling. Lets say you want the following,
|
## Using `?` in doc tests
|
||||||
|
|
||||||
|
A complete error handling is often not useful in your example, as it would add
|
||||||
|
significant amounts of boilerplate code. Instead, you may want the following:
|
||||||
|
|
||||||
```ignore
|
```ignore
|
||||||
|
/// ```
|
||||||
/// use std::io;
|
/// use std::io;
|
||||||
/// let mut input = String::new();
|
/// let mut input = String::new();
|
||||||
/// io::stdin().read_line(&mut input)?;
|
/// io::stdin().read_line(&mut input)?;
|
||||||
|
/// ```
|
||||||
```
|
```
|
||||||
|
|
||||||
The problem is that `?` returns a `Result<T, E>` and test functions
|
The problem is that `?` returns a `Result<T, E>` and test functions don't
|
||||||
don't return anything so this will give a mismatched types error.
|
return anything, so this will give a mismatched types error.
|
||||||
|
|
||||||
|
You can get around this limitation by manually adding a `main` that returns
|
||||||
|
`Result<T, E>`, because `Result<T, E>` implements the `Termination` trait:
|
||||||
|
|
||||||
```ignore
|
```ignore
|
||||||
/// A doc test using ?
|
/// A doc test using ?
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::io;
|
/// use std::io;
|
||||||
/// # fn foo() -> io::Result<()> {
|
///
|
||||||
|
/// fn main() -> io::Result<()> {
|
||||||
|
/// let mut input = String::new();
|
||||||
|
/// io::stdin().read_line(&mut input)?;
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
```
|
||||||
|
|
||||||
|
Together with the `# ` from the section above, you arrive at a solution that
|
||||||
|
appears to the reader as the initial idea but works with doc tests:
|
||||||
|
|
||||||
|
```ignore
|
||||||
|
/// ```
|
||||||
|
/// use std::io;
|
||||||
|
/// # fn main() -> io::Result<()> {
|
||||||
/// let mut input = String::new();
|
/// let mut input = String::new();
|
||||||
/// io::stdin().read_line(&mut input)?;
|
/// io::stdin().read_line(&mut input)?;
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
# fn foo() {}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You can get around this by wrapping the code in a function. This catches
|
## Documenting macros
|
||||||
and swallows the `Result<T, E>` when running tests on the docs. This
|
|
||||||
pattern appears regularly in the standard library.
|
|
||||||
|
|
||||||
### Documenting macros
|
|
||||||
|
|
||||||
Here’s an example of documenting a macro:
|
Here’s an example of documenting a macro:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue