Always use quick_main

This commit is contained in:
David Tolnay 2017-05-17 10:09:13 -07:00
parent 78463b1718
commit 9467df216a
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82

View file

@ -26,34 +26,18 @@ practices, they set up error handling correctly when there are
`Result` types involved.
The basic pattern we use is to have a `fn run() -> Result` that acts
like the "real" main function.
like the "real" main function. We use the [error-chain] crate to make
`?` work within `run`.
The code for this setup generally looks like:
The structure generally looks like:
```rust
use std::io::{self, Write};
fn run() -> io::Result<()> {
writeln!(io::stderr(), "hello, world")?;
Ok(())
}
fn main() {
run().unwrap();
}
```
and when necessary to reduce boilerplate,
they use the [error-chain] crate.
```rust
use std::net::IpAddr;
use std::str;
#[macro_use]
extern crate error_chain;
use std::net::IpAddr;
use std::str;
error_chain! {
foreign_links {
Utf8(std::str::Utf8Error);
@ -78,10 +62,10 @@ quick_main!(run);
```
This is using the `error_chain!` macro to define a custom `Error` and
`Result` type, along with an automatic conversion from two standard
library error types. The automatic conversion makes the `?` operator
work. The `quick_main!` macro is also used to generate the boilerplate `main`
from above.
`Result` type, along with automatic conversions from two standard
library error types. The automatic conversions make the `?` operator
work. The `quick_main!` macro generates the actual `main` function and
prints out the error if one occurred.
For more background on error handling in Rust, read [this page of the
Rust book][error-docs] and [this blog post][error-blog].