Start learning free

LESSON 81 OF 348 · CONDITIONAL STATEMENTS

Logical Operators: Or (||) in Rust

Lesson

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 false
  • 10 < 12, which evaluates to true
As at least one of the conditions evaluates to true, the entire line will evaluate 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.


STEP 1 OF 2
    Ctrl + Enter to check

    Or Operator in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free