Comparison of The Different Conditional Constructs

This lesson compares the different conditional constructs we have studied so far.

Use if Statement

Use an if statement if:

  • It is desired to test the truthiness of an expression
  • There is a single affirmative test
  • There is a need to evaluate different expressions for each branch
  • It can test expressions based on ranges of values or conditions

Use match Statement

Use a match statement if:

  • It is desired to compare multiple possible conditions of an expression
  • The values belong to the same domain
  • It can test expressions based on values only, i.e., condition cannot take ranges

Use if let Statement

Use an if let statement if:

  • There is a pattern in the condition and it is to be matched with the scrutinee expression

Now that you have learned all about conditional statements in Rust, check your knowledge through a challenge in the upcoming lesson.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy