4 ms·
Wasn't there an attempt to get a variation of main that returned result at one point(that would presumably print error and set exit code one error)? Back before
by rat87 10y ago
Wasn't there an attempt to get a variation of main that returned result at one point(that would presumably print error and set exit code one error)? Back before "?"
If rust had that examples would be even shorter using ? Instead of try! or unwrap
- steveklabnik 10y agoYes, and that discussion is still ongoing. The devil, as always, is in the details.
- JoshTriplett 10y ago`-> impl Trait` seems useful in that context, so that main could have a variety of appropriate return value types: `()`, `i32` (for an exit code), or `Result<T, E>` where T implements ReturnValue and E implements Error. `quick_error` uses a trait like that to determine the exit code of `main()`, allowing it to return either () or i32.
- masklinn 10y agoFWIW there are third-party libraries which include that feature e.g. error_chain has a `quick_main` macro (working with its `error_chain!`-generated error, but I'm not sure it actually depends on it): quick_main!(|| -> Result<()> { // insert Result-returning code here }); Alternatively it takes a few lines to bootstrap it by hand e.g. ripgrep uses this code to bootstrap: fn main() { match Args::parse().map(Arc::new).and_then(run) { Ok(0) => process::exit(1), Ok(_) => process::exit(0), Err(err) => { eprintln!("{}", err); process::exit(1); } } } https://github.com/BurntSushi/ripgrep/blob/master/src/main.rs#L59 https://github.com/BurntSushi/ripgrep/blob/master/src/main.r...