Industrial Training




Concise control flow with if let

The if let syntax is used to combine if and let which handles the values that matches one of the patterns while ignoring the rest of the code. The working of "match" operator and "if let" expression is similar.


Example of match operator


 fn main()  
{  
let a = Some(5);  
match a {  
    Some(5) => println!("five"),  
    _ => (),  
}}  

Output:
five

In the above example, the match operator executes the code when the value is equal to Some(5). The "_=>()" expression satisfies the match expression after executing the first variant. If we use if let instead of match, then it reduces the length of the code.


Example of if let


 fn main()  
{  
let a=Some(3);  
if let Some(3)=a{  
 println!("three");  
   }  
   }  

Output:
three



Hi I am Pluto.