Start with the idea
Because if is an expression, both branches must produce the same type — there is no silent conversion to paper over a mismatch. loop, while, and for then express three different repetition rules: forever, until a condition fails, and over an iterator.
Read the code
let value: i32 = 6;
let label = if value % 2 == 0 {
"even"
} else {
"odd"
};
for index in 0..3 {
println!("{index}: {label}");
}if is an expression, so both arms must agree on a type.
Practice starter
let flag = true;
let value = if flag { 1 } else { "two" };
Work through it
In the example, value % 2 == 0 evaluates to bool. Both if branches produce a &str, so the entire if expression can initialize label. If one branch returned a number instead, rustc would reject the assignment because a single expression cannot have two unrelated result types. This is a useful discipline: deciding what label means happens in one place, and the loop can simply print it.
The range 0..3 supplies 0, 1, and 2 to for. Each iteration binds index to the next value and prints one line. The loop does not need a separate counter mutation or a manual exit condition. Later you will see while for a condition that changes over time and loop for cases where break itself decides when to stop. Here, for expresses the finite sequence directly.
A match expression can provide the same kind of value selection when there are more than two cases. Every arm must be covered, and all arms used as one expression must agree on a result type. Predict the three output lines before running, including their order. Then change one branch from "even" to 0 and explain the diagnostic in terms of the type of label, rather than treating it as an arbitrary compiler restriction.
One subtlety is that if let is attractive for a single interesting shape, while match makes every alternative explicit. If a branch is used as a value, confirm that all paths produce the same type before deciding where the semicolon belongs. Test the boundary condition itself, not only values comfortably on either side.
An if chooses a branch from a boolean condition; match checks which pattern a value has; loops repeat work. Rust does not guess whether a number or string should count as true. This makes the decision visible at the line where it happens. When an if supplies a value, both branches must agree on a type. When it only performs side effects, returning () is fine.
Trace the sample with at least two different inputs. For each, write the condition's value, the branch selected, and the value produced. Then replace a long chain of related conditions with match and ask whether all cases are covered. The compiler helps check exhaustive matches, but it cannot decide whether your branch rule is the one a user expects. Put boundary inputs in your examples: equal to a threshold, one below it, and one above it. Most mistakes in a branch are at those boundaries.
Try it
Return negative, zero, or positive according to the integer's sign. All branches return a string slice. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Why do those two branches not type-check?
Reveal the answer
- The condition must be an integer, not a bool
- The branches produce different types
- An if expression cannot be bound with let
An if expression has one type, so every branch has to match.
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: Control Flow (courseId "basics", lessonId "control-flow") Link: https://crabrix.com/learn/basics/control-flow/ Goal: if is an expression; every branch must produce the same type. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/control-flow) 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.