| Syntax |
enum enum_name {
variant1,
variant2,
variant3
}
|
| Using an Enumeration |
|
the example declares an enum which has variants as Male and Female the print! macro displays value of the enum the compiler will throw an error the trait std::fmt::Debug is not implemented for GenderCategorythe attribute #[derive(Debug)] is used to suppress this error // The 'derive' attribute automatically creates the implementation
// required to make this 'enum' printable with 'fmt::Debug'.
#[derive(Debug)]
enum GenderCategory {
Male,Female
}
fn main() {
let male = GenderCategory::Male;
let female = GenderCategory::Female;
println!("{:?}",male);
println!("{:?}",female);
}
|
| Struct and Enum |
|
the example defines a structure Person the field gender is of the type GenderCategory (which is an enum) and can be assigned either Male or Female as value // The 'derive' attribute automatically creates the
implementation
// required to make this 'enum' printable with
'fmt::Debug'.
#[derive(Debug)]
enum GenderCategory {
Male,Female
}
// The 'derive' attribute automatically creates the implementation
// required to make this 'struct' printable with 'fmt::Debug'.
#[derive(Debug)]
struct Person {
name:String,
gender:GenderCategory
}
fn main() {
let p1 = Person {
name:String::from("Mohtashim"),
gender:GenderCategory::Male
};
let p2 = Person {
name:String::from("Amy"),
gender:GenderCategory::Female
};
println!("{:?}",p1);
println!("{:?}",p2);
}
|
| Option Enum |
|
Option is a predefined enum in the Rust standard library has two values
enum OptionRust does not support the null keyword the value None can be used by a function to return a null value if there is data to return, the function can return Some(data) example defines a function is_even() with a return type Option function verifies if the value passed is an even number if the input is even then a value true is returned, else the function returns None fn main() {
let result = is_even(3);
println!("{:?}",result);
println!("{:?}",is_even(30));
}
fn is_even(no:i32)->Option
output
None Some(true) |
| Match Statement and Enum |
|
the match statement can be used to compare values stored in an enum example defines a function, print_size, which takes CarType enum as parameter function compares the parameter values with a pre-defined set of constants and displays the appropriate message enum CarType {
Hatch,
Sedan,
SUV
}
fn print_size(car:CarType) {
match car {
CarType::Hatch => {
println!("Small sized car");
},
CarType::Sedan => {
println!("medium sized car");
},
CarType::SUV =>{
println!("Large sized Sports Utility car");
}
}
}
fn main(){
print_size(CarType::SUV);
print_size(CarType::Hatch);
print_size(CarType::Sedan);
}
output
Large sized Sports Utility car Small sized car medium sized car |
| Match with Option |
the example of is_even function, which returns Option type, can also be implemented with match statement
fn main() {
match is_even(5) {
Some(data) => {
if data==true {
println!("Even no");
}
},
None => {
println!("not even");
}
}
}
fn is_even(no:i32)->Option
|
| Match & Enum with Data Type |
|
is possible to add data type to each variant of an enum in the example Name and Usr_ID variants of the enum are of String and integer types respectively example shows the use of match statement with an enum having a data type // The 'derive' attribute automatically creates the implementation
// required to make this 'enum' printable with 'fmt::Debug'.
#[derive(Debug)]
enum GenderCategory {
Name(String),Usr_ID(i32)
}
fn main() {
let p1 = GenderCategory::Name(String::from("Mohtashim"));
let p2 = GenderCategory::Usr_ID(100);
println!("{:?}",p1);
println!("{:?}",p2);
match p1 {
GenderCategory::Name(val)=> {
println!("{}",val);
}
GenderCategory::Usr_ID(val)=> {
println!("{}",val);
}
}
}
output
Name("Mohtashim")
Usr_ID(100)
Mohtashim
|