How to do it...

Perform the following steps:

  1. 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);
}
  1. Declare an enum date type, namely Hero:
        // declaring the enum 
enum Hero {
Fast,
Strong(i32),
Info {name : String, secret : String}
}
  1. 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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161