Enum to represent exit status
-
We define a type called
ExitCode
that has two variants:Success
andFailure
. -
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
Debug
trait for an arbitraryenum
so wederive
from theDebug
trait 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:?}"); }