DEV Community

Cover image for Day 5: Mastering Rust's Conditionals and Match Expressions
Aniket Botre
Aniket Botre

Posted on

Day 5: Mastering Rust's Conditionals and Match Expressions

Greetings, fellow coders! πŸš€ On Day 5 of my #100DaysOfCode adventure with Rust, I delved into the art of conditionals, exploring if statements and the powerful match control flow construct. Let's unravel the intricacies of Rust's decision-making capabilities.

If Statements: Navigating Conditional Paths

In Rust, if statements provide a flexible way to make decisions based on conditions. The syntax is familiar, but Rust's emphasis on safety shines through:

let num: i32 = 4;

if num % 2 == 0 {
    println!("{num} is an even number!");
} else if num < 0 {
    println!("Enter a positive number!");
} else {
    println!("{num} is an odd number!");
}
// Output: 4 is an even number!
Enter fullscreen mode Exit fullscreen mode

Key points about Rust's if statements:

  • The if expression starts with the keyword, followed by a condition.

  • The code inside the curly braces {} is executed if the condition is true.

  • else and else if blocks are optional but provide additional branching logic.

  • Conditions are checked sequentially, and only the first true condition's code block is executed.

We can even embed if...else conditions inside let statements for concise code:

let age = 20;
let result = if age > 18 {
    "You can drive"
} else {
    "You cannot drive!"
};
println!("{result}");
// Output: You can drive
Enter fullscreen mode Exit fullscreen mode

Match Control Flow: Rust's Versatile Coin-Sorting Machine

When dealing with multiple conditions, the match keyword becomes a powerful ally. It compares a value against a series of patterns and executes code based on the matching pattern:

let num = 3;

match num {
    1 => println!("One"),
    2 => println!("Two"),
    3 => println!("Three"),
    _ => println!("Something else"),
}
// Output: Three
Enter fullscreen mode Exit fullscreen mode

Key features of Rust's match expressions:

  • Patterns are compared sequentially, and the first matching pattern triggers the corresponding code block.

  • The _ (underscore) represents the default case when no match is found.

  • The match expression must be exhaustive, covering all possible values.

Multiple Values in a SingleΒ Case

We can consolidate multiple values into a single case for cleaner code:

let num: i32 = 4;

match num {
    0 | 2 | 4 | 6 | 8 | 10 => println!("{num} is even**!"),
    1 | 3 | 5 | 7 | 9 => println!("{num} is odd**!"),
    _ => println!("Number should be between 0 and 10!!"),
}
// Output: 4 is even**!
Enter fullscreen mode Exit fullscreen mode

Leveraging Ranges in Match Expressions

Ranges add elegance to match expressions, enabling concise comparisons:

let age = 20;

match age {
    4..=15 => println!("Child"),
    16..=20 => println!("Teenage"),
    21..=30 => println!("Adult"),
    _ => println!("Invalid age"),
}
// Output: Teenage
Enter fullscreen mode Exit fullscreen mode

As I traverse Rust's terrain of conditionals, each statement and match expression unveils the language's precise and safety-oriented nature. Follow my coding journey on my Github repo for more updates! πŸ’»πŸŒβœ¨

RustLang #Programming #LearningToCode #CodeNewbie #TechJourney #Day5

Top comments (0)