Enum to represent exit status
-
We define a type called
ExitCodethat has two variants:SuccessandFailure. -
We can then, based on the results of our porgram set the value of exit.
-
However, this is a bit limited as we can only indicate failure without the details we are used to - which is a number.
-
By default Rust does not implement the
Debugtrait for an arbitraryenumso wederivefrom theDebugtrait to be able to print the values using the:?placeholder. -
We can also observe that
if-statements can have a return value.
#[derive(Debug)]
enum ExitCode {
Success,
Failure,
}
fn main() {
let args = std::env::args().collect::<Vec<_>>();
let exit = if args.len() > 1 {
ExitCode::Success
} else {
ExitCode::Failure
};
println!("{exit:?}");
}