Perform the following steps:
- Create a file named enum.rs and enter the following code in the script:
fn main() {
let hulk = Hero::Strong(100);
let fasty = Hero::Fast;
//converting from
let spiderman = Hero::Info
{name:"spiderman".to_owned(),secret:"peter
parker".to_owned()};
get_info(spiderman);
get_info(hulk);
get_info(fasty);
}
- Declare an enum date type, namely Hero:
// declaring the enum
enum Hero {
Fast,
Strong(i32),
Info {name : String, secret : String}
}
- Create a function named get_info that will take the enum data type as an argument:
// function to perform for each types
fn get_info(h:Hero){
match h {
Hero::Fast => println!("Fast"),
Hero::Strong(i) => println!("Lifts {} tons",i ),
Hero::Info {name,secret} => { println!(" name is: {0} secret is
: {1}", name,secret);} ,
}
}
You should get the following screenshot as output upon running the preceding code: