diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-11-10 10:51:47 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-11-10 10:51:47 -0500 |
| commit | fb2ae896bb1d6ec1af7ad82ec834d25cb55eda16 (patch) | |
| tree | 90a974a006cefd82bc48e50670c1554ff9609daf | |
| parent | e9aa32ac728b297ed8f62e592e1de5ac86b6e4a3 (diff) | |
| parent | dda7a3c2a195b7d99f4173f3636a8a7d25ca8c0c (diff) | |
| download | rust-fb2ae896bb1d6ec1af7ad82ec834d25cb55eda16.tar.gz rust-fb2ae896bb1d6ec1af7ad82ec834d25cb55eda16.zip | |
Rollup merge of #29420 - efindlay:master, r=steveklabnik
r? @steveklabnik
| -rw-r--r-- | src/doc/trpl/documentation.md | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/doc/trpl/documentation.md b/src/doc/trpl/documentation.md index e101d4bc0d4..dc91c90b0fd 100644 --- a/src/doc/trpl/documentation.md +++ b/src/doc/trpl/documentation.md @@ -373,6 +373,36 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own `main()` as well. Finally, a judicious use of `#` to comment out those two things, so they don’t show up in the output. +Another case where the use of `#` is handy is when you want to ignore +error handling. Lets say you want the following, + +```rust,ignore +/// use std::io; +/// let mut input = String::new(); +/// try!(io::stdin().read_line(&mut input)); +``` + +The problem is that `try!` returns a `Result<T, E>` and test functions +don't return anything so this will give a mismatched types error. + +```rust,ignore +/// A doc test using try! +/// +/// ``` +/// use std::io; +/// # fn foo() -> io::Result<()> { +/// let mut input = String::new(); +/// try!(io::stdin().read_line(&mut input)); +/// # Ok(()) +/// # } +/// ``` +# fn foo() {} +``` + +You can get around this by wrapping the code in a function. This catches +and swallows the `Result<T, E>` when running tests on the docs. This +pattern appears regularly in the standard library. + ### Running documentation tests To run the tests, either: |
