Skip to content

Added 'unwrap_or_exit'.

Jeremy Soller requested to merge goyox86:master into master

Created by: goyox86

In the coreutils and userutils and pretty much all the crates that implement CLI applications is very common to execute system calls or other library code that returns optional types and inmediately exit the program in case of an None or an Err is found displaying a message to STDERR with something like "foo: file not found" where 'foo' is the utility.

In code it looks like this:

let user = get_user_by_id(uid).unwrap_or_else(|err| {
  eprintln!("passwd: {}", err);
  exit(1);
})

And after this commit we could do:

let user = get_user_by_id(uid).unwrap_or_exit(1);

@MggMuggins came with a proposal in https://github.com/redox-os/libextra/pull/10 (in which this work is based on) and after discussing we agreed to implement unwrap_or_exit.

Merge request reports