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
ifchecks ifxis greater than 5 andyis less than 30. - The second
ifchecks ifxis less than 5 oryis 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.
