Enum is used to create a new type by selecting one of the predefined constant values.
Rust Enum
Here is a syntax for Enum
enum enum_name{
VALUE1,
VALUE2,
----
VALUEN
}
#[derive(Debug)]
enum Status {
ACTIVE,
INACTIVE,
}
fn main() {
let active = Status::ACTIVE;
let inactive = Status::INACTIVE;
println!("{:?}", active);
println!("{:?}", inactive);
}
Output:
ACTIVE
INACTIVE
#[derive(Debug)] statement was added to avoid the below compilation error. As Enums can not be formatted with {:?}
println!("{:?}", inactive);
| ^^^^^^^^ Status cannot be formatted using {:?}
|
= help: the trait Debug is not implemented for Status
= note: add #[derive(Debug)] to Status or manually impl Debug for Status
= note: this error originates in the macro $crate::format_args_nl (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors