Rust Foundations · 10 min · free lesson

Booleans

A comparison produces a bool, and a branch requires one. && and || can skip their right-hand expression as soon as the result is known.

Core idea

if requires bool; Rust has no implicit numeric truthiness.

Coming from Node.js? Truthiness vs bool, if and match covers this side by side.

Start with the idea

A condition is not an arbitrary number or string in Rust. Comparisons such as builds > 0 and errors == 0 each produce a bool. The example combines them into ready, a name that expresses the decision the program needs to make. That makes the if branch easy to inspect: it runs only after at least one build exists and no errors remain. If your code says if count, rustc asks for a real boolean condition instead of guessing whether zero should mean false.

Read the code

let builds = 4;
let errors = 0;
let ready = builds > 0 && errors == 0;
if ready {
    println!("ready");
}

The name says what must be true before work starts.

Practice starter

let count = 3;
if count {
    println!("items");
}

Practise this code in Playground ↗

A comparison produces a bool; false skips the right side of &&, while true skips the right side of ||.
Short-circuit operators evaluate only the right-hand expression needed to determine the result.

Work through it

The logical operators also control which work is performed. For left && right, a false left side settles the answer, so right is not evaluated. For left || right, a true left side settles it. This is short-circuiting, shown in the diagram by the crossed-out right branch. It matters when the right expression is expensive or valid only after the left has checked a precondition. Do not use side effects on the right merely because you expect them to run: the operator may skip them. Predict the result for each pair of boolean values, then use ! to reverse one condition and check that the branch still says what you intend.

You can use short-circuiting as a safe guard, for example checking that an index is in range before reading an element on the right side of &&. Reverse the order and the protection disappears. Parentheses help when a condition mixes && and ||: write the grouping you mean instead of making a future reader remember precedence. A descriptive name such as ready often makes the decision clearer than repeating the whole expression at every if.

A boolean has exactly two values: true and false. A comparison such as age >= 18 produces a boolean; an if condition requires one. Rust does not treat 0, an empty string, or a null pointer as false. That explicitness makes the meaning of a branch visible. Use == to compare and = to assign; confusing them produces a compiler diagnostic rather than a silent change of logic.

&& means both conditions must be true. || means at least one must be true. They short-circuit: for left && right, Rust skips right if left is false; for left || right, it skips right if left is true. This matters when the right side is expensive or could be invalid without the left-side guard. For example, check that a collection is nonempty before accessing a first element. Parentheses can make a mixed expression easier to read even when precedence rules would choose the same grouping.

Draw a two-column truth table for two conditions such as has_ticket and is_open. Predict the result of has_ticket && is_open for all four input pairs. Then change && to || and explain which rows changed. Finally put a visible print in the right-hand expression and observe when it is skipped. If the result surprises you, evaluate each comparison first, then combine the booleans; do not reason from the English sentence alone.

Try it

Build a clearly named condition from two comparisons. Predict whether the right side of && runs when the left is false, and whether the right side of || runs when the left is true.

What success looks like: The branch runs only for the intended combination of values. You can explain why each skipped right-hand expression was unnecessary.

Check your understanding

What must replace count in the condition?

Reveal the answer
  1. A comparison such as count > 0
  2. count as bool
  3. Nothing; every nonzero integer is true

Write the condition you mean explicitly, such as count > 0.

One more experiment

Set errors to 1 and predict the stdout. Then change && to || and predict again. Write both results before asking rustc to build and run them.

Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Booleans (courseId "basics", lessonId "booleans-comparisons")
Link: https://crabrix.com/learn/basics/booleans-comparisons/
Goal: if requires bool; Rust has no implicit numeric truthiness.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/booleans-comparisons) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.