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.
| left | right | left || right |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
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 noorkeyword. Use||. - Naming the variable once. To check that
xis 3 or 5, writex == 3 || x == 5. The formx == 3 || 5is a type error, because5is not abool. - Using
||for a range.x >= 5 || x <= 9is true for every number. A range needs&&, as inx >= 5 && x <= 9. - Integer conditions. An
ifneeds a realbool; 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.
- Declare
weatheras"sunny"andtemperatureas90withlet. - Write the
if. The condition is true whenweatherdiffers from"sunny"ortemperatureis at most 85:weather != "sunny" || temperature <= 85. Comparing&strvalues with!=compares their text. - Print
"Learning to code"in that block withprintln!. - Write
} else {on the closing line and print"Going to the beach". Theelseneeds 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 || bistruewhen at least one ofaandbistrue.- The right side is skipped when the left side is already
true. - Rust spells it
||, neveror, and both sides must bebool. !(a && b)is the same as!a || !b.
