Right now, to write Unix CLI utilities in Rust, you wind up doing something like this:
fn inner_main() -> Result<(), HLError> {
let args = parse_cmdline()?;
// all the real work here
}
fn main() {
process::exit(match inner_main() {
Ok(_) => 0,
Err(ref e) => {
writeln!(io::stderr(), "{}", e).unwrap();
1
}
});
}
So I like the fn main () -> something_Result_ish
proposal, because it basically paves this cowpath.
However. It is very important for this use case that returning an Err
from main
does not trigger a panic. It normally would not represent a bug, and in some cases, it needs to produce no output other than the exit code –
$ grep -q root /etc/passwd ; echo $?
0
$ grep -q notthere /etc/passwd ; echo $?
1
$ grep -q notthere /etc/shadow ; echo $?
grep: /etc/shadow: permission denied
2