Comparison and logical operators
Quick answer
Comparison operators produce a boolean: ==, !=, <, <=, > and >=. Combine boolean conditions with && (both), || (either) and ! (not). Use them to express eligibility or validity checks.
Short-circuit logic
With &&, the right operand is evaluated only if the left is true. With ||, it is evaluated only if the left is false. Use this rule to protect operations that require a precondition. Conditions must be booleans; numbers are not automatically truth values.
fn main() { let used = 7; let limit = 10; let open = true; println!("Can enter: {}", used < limit && open);}Expected stdout (not executed):
Can enter: trueBoth required conditions are true.
fn main() { let mut evaluated = false; let valid = false && { evaluated = true; true }; println!("Valid: {valid}, right evaluated: {evaluated}");}Expected stdout (not executed):
Valid: false, right evaluated: falseThe right block never executes because the left operand is false. The block syntax is explained in the next chapter section.
Common mistake
Use == for equality, not =. Avoid mathematical chained comparisons such as 0 < x < 10; combine two complete comparisons with &&.
Try it yourself
Write a boolean expression that checks whether a score is between 0 and 100 inclusive.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Comparison and logical operators — practice quiz
Choose one answer per question, then review the explanation.
What is `!(3 == 3)`?
When the left operand of logical OR is true, what happens to the right operand?
Which expression checks that age is at least 18 and that an account is active?