Sometimes we will need to run some code if at least one of two conditions is true. The logical or operator (||) returns true if either of the conditions is true. Otherwise, if both conditions are false, it returns false.
For example:
7 >= 10 || 10 < 12 // Evaluates to true
We have two conditions separated by || operator:
7 >= 10, which evaluates to false10 < 12, which evaluates to true
Another example:
10 < 15 || 10 > 9 // Evaluates to true as both conditions are true
An example inside an if statement:
let x = 10;
if x != 10 || 12 < x { // Evaluates to false
println!("This is true!");
}
Both conditions evalute to false, so the entire condition evaluates to false and the program prints nothing.
Assignment
Follow the Coding Tutorial and play with the or operator.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.