Start learning free

LESSON 83 OF 414 · CONDITIONAL STATEMENTS

Or Operator in Rust: Combining Conditions with ||

Lesson Read the full explanation

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 evaluate 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 3
    Ctrl + Enter to check

    RUST · CONDITIONAL STATEMENTS

    Or Operator in Rust, explained

    What the or operator does

    The logical or operator, written ||, joins two bool expressions and gives true when at least one of them is true. It gives false only when both are false.

    leftrightleft || right
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    For example, 7 >= 10 || 10 < 12 is true. The left comparison is false, since 7 is smaller than 10, but the right one is true, and one true side is enough. A case where both sides are true, such as 10 < 15 || 10 > 9, also gives true: "or" here is inclusive, not "one or the other but not both".

    Using it in an if

    let x = 10;
    if x != 10 || 12 < x {
        println!("This is true!");
    }
    

    Here x != 10 is false and 12 < x is false. With both sides false, the condition is false, and the program prints nothing.

    The result is a value

    An || expression produces a bool like any other expression. You can store it with let either = 7 >= 10 || 10 < 12;, which makes either true, print it, return it from a function, or use it inside a larger condition. The if is just where it is needed most often.

    More than two conditions

    Like &&, the operator chains: a || b || c is true when any one of the three is true. A typical use is a list of acceptable values, such as day == "Sat" || day == "Sun", which is true for either weekend day and false for every other string.

    Short-circuiting

    Rust evaluates || from left to right and stops as soon as the answer is known. If the left side is true, the result is true whatever the right side is, so the right side is never evaluated. With a function check that prints a message when it runs, check("c", true) || check("d", true) prints only checked c. The call for "d" never happens.

    This is the mirror image of &&, which skips its right side when the left one is false. A single | on two booleans also computes "or", but it evaluates both sides, so conditions normally use ||.

    Or as the opposite of and

    An || condition is the negation of an && condition with the parts flipped. "Not both sunny and hot" is the same as "not sunny, or not hot". In code, !(a && b) equals !a || !b. The lesson uses this: its condition weather != "sunny" || temperature <= 85 is the exact opposite of weather == "sunny" && temperature > 85. Each comparison is reversed (== becomes !=, > becomes <=) and the operator changes from and to or.

    Common mistakes

    • Writing or. Rust has no or keyword. Use ||.
    • Naming the variable once. To check that x is 3 or 5, write x == 3 || x == 5. The form x == 3 || 5 is a type error, because 5 is not a bool.
    • Using || for a range. x >= 5 || x <= 9 is true for every number. A range needs &&, as in x >= 5 && x <= 9.
    • Integer conditions. An if needs a real bool; Rust does not treat numbers as true or false.

    The lesson's exercise

    The exercise reverses the previous one: it goes to learn to code if the weather is not sunny or the temperature is low.

    1. Declare weather as "sunny" and temperature as 90 with let.
    2. Write the if. The condition is true when weather differs from "sunny" or temperature is at most 85: weather != "sunny" || temperature <= 85. Comparing &str values with != compares their text.
    3. Print "Learning to code" in that block with println!.
    4. Write } else { on the closing line and print "Going to the beach". The else needs no condition. It runs when both comparisons are false, which means sunny weather and a temperature above 85.
    let weather = "sunny";
    let temperature = 90;
    if weather != "sunny" || temperature <= 85 {
        println!("Learning to code");
    } else {
        println!("Going to the beach");
    }
    

    Here weather != "sunny" is false and temperature <= 85 is false (90 is above 85). Neither side is true, so the program prints Going to the beach. If temperature were 80, the second comparison would be true and the output would be Learning to code.

    Key takeaways

    • a || b is true when at least one of a and b is true.
    • The right side is skipped when the left side is already true.
    • Rust spells it ||, never or, and both sides must be bool.
    • !(a && b) is the same as !a || !b.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free