How to add more conditions?

To add more conditions in Rust, you can use logical operators such as && (and) and || (or) within your conditional statements. Here's an example using an if statement with multiple conditions:

fn main() {
    let x = 10;
    let y = 20;

    if x > 5 && y < 30 {
        println!("Both conditions are true!");
    }

    if x < 5 || y > 15 {
        println!("At least one condition is true!");
    }
}

In this example:

  • The first if checks if x is greater than 5 and y is less than 30.
  • The second if checks if x is less than 5 or y is greater than 15.

You can also use match statements for more complex conditions:

fn main() {
    let number = 7;

    match number {
        1..=5 => println!("Number is between 1 and 5"),
        6..=10 => println!("Number is between 6 and 10"),
        _ => println!("Number is greater than 10"),
    }
}

In this match example, different ranges are checked, allowing for multiple conditions to be handled in a clean way.

0 Comments

no data
Be the first to share your comment!