Skip to content

Commit fb2ae89

Browse files
committed
Rollup merge of rust-lang#29420 - efindlay:master, r=steveklabnik
r? @steveklabnik
2 parents e9aa32a + dda7a3c commit fb2ae89

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/doc/trpl/documentation.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,36 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
373373
`main()` as well. Finally, a judicious use of `#` to comment out those two
374374
things, so they don’t show up in the output.
375375

376+
Another case where the use of `#` is handy is when you want to ignore
377+
error handling. Lets say you want the following,
378+
379+
```rust,ignore
380+
/// use std::io;
381+
/// let mut input = String::new();
382+
/// try!(io::stdin().read_line(&mut input));
383+
```
384+
385+
The problem is that `try!` returns a `Result<T, E>` and test functions
386+
don't return anything so this will give a mismatched types error.
387+
388+
```rust,ignore
389+
/// A doc test using try!
390+
///
391+
/// ```
392+
/// use std::io;
393+
/// # fn foo() -> io::Result<()> {
394+
/// let mut input = String::new();
395+
/// try!(io::stdin().read_line(&mut input));
396+
/// # Ok(())
397+
/// # }
398+
/// ```
399+
# fn foo() {}
400+
```
401+
402+
You can get around this by wrapping the code in a function. This catches
403+
and swallows the `Result<T, E>` when running tests on the docs. This
404+
pattern appears regularly in the standard library.
405+
376406
### Running documentation tests
377407

378408
To run the tests, either:

0 commit comments

Comments
 (0)