Hacker News new | comments | show | ask | jobs | submit login

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






FWIW 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.r...

Yes, and that discussion is still ongoing. The devil, as always, is in the details.

`-> 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.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: