loading

If Let Operator

Concise control flow with if let

Combining if and let allows you to handle values that match one of the patterns while disregarding the remaining code. This is done with the if let syntax. The “if let” expression and the “match” operator operate similarly.

Example of match operator

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

Output:

				
					five
				
			

When the value in the example above equals Some(5), the match operator runs the code. After running the first variant, the “_=>()” expression satisfies the match expression. The length of the code is decreased if we use if let in instead of match.

Example of if let

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

Output:

				
					three
				
			
Share this Doc

If Let Operator

Or copy link

Explore Topic