HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
🔁 Condition & Loop
🧩 Function
🔑 Ownership & Borrowing
Rust›Conditions (if / match)

Conditions (if / match)

Parentheses-free if-else and powerful pattern matching (match).

Simple enough for a 5-year-old to understand

if / else

In Rust, conditions are written WITHOUT parentheses, but { } are mandatory.

Example code
1
fn main() {
2
let score = 75;
3
if score >= 50 {
No parentheses.
ConditionNo parentheses.
4
println!("Passed");
5
} else {
6
println!("Failed");
7
}
8
}
Condition

About this topic

In Rust, the condition MUST be a BOOLEAN value. Writing a number as a condition is a compilation error; there is no rule where zero counts as false. This cuts off a bunch of silent bugs coming from the C family right from the start.

Condition parentheses are also not required, but block curly braces are MANDATORY. Thus, the classic mistake in C — forgetting the curly braces and adding a second line later — is impossible in Rust.

Rust's most powerful tool in conditions is the match expression. It is similar to switch but FORCES you to handle all possibilities: if you skip a case, the code won't compile. This feature completely eliminates bugs caused by forgotten cases.

What you will learn

  • the if structure and the fact that the condition must be boolean
  • the use of if as an expression
  • the match expression and exhaustiveness checking
  • extracting values with if let

When to use?

match is used when a value takes one of a limited number of states; the compiler does not allow you to skip a case and shows you the missing case when the code is extended later.

if let is shorter and more readable when you are only checking if a value exists.

Common mistakes

Wrong

fn main() {
    let count = 3;
    if count {
        println!("Not zero");
    }
}

The condition must be a boolean value; the compiler gives an "expected bool, found integer" error. There is no rule in Rust where zero counts as false, and this is intentional: writing the comparison explicitly frees the intention from ambiguity.

Correct

fn main() {
    let count = 3;
    if count != 0 {
        println!("Not zero");
    }
}

Wrong

fn main() {
    let level = 2;
    let name = match level {
        1 => "beginner",
        2 => "intermediate",
    };
    println!("{}", name);
}

match must cover all possibilities; since other values of the integer are not handled, the compiler gives a "non-exhaustive patterns" error. The underscore arm catches all remaining cases. This requirement makes forgotten case bugs impossible.

Correct

fn main() {
    let level = 2;
    let name = match level {
        1 => "beginner",
        2 => "intermediate",
        _ => "unknown",
    };
    println!("{}", name);
}

Wrong

fn main() {
    let age = 20;
    let status = if age >= 18 { "adult" } else { 0 };
    println!("{}", status);
}

When if is used as an expression, all arms MUST have the SAME type; because one is text and the other is a number, the compiler gives an "if and else have incompatible types" error. This is mandatory because a variable must have a single type.

Correct

fn main() {
    let age = 20;
    let status = if age >= 18 { "adult" } else { "child" };
    println!("{}", status);
}

Frequently asked questions

why does match require all cases?

Because a skipped case is the most common type of bug in practice. More importantly: when a new case is added to your code, the compiler points out every match that doesn't handle it. This is the primary tool for safely modifying code in large projects.

when is if let used?

Only when you care about a single case. If you want to check if a value exists and use it if it does, writing a match is unnecessarily long; if let does the same job in two lines and ignores other cases.

Should I use parentheses in the condition?

Not required, and writing them produces a compiler warning. Since Rust makes block curly braces mandatory, it is already clear where the condition ends. If you are coming from C, you might write them out of habit, but the compiler will tell you they are unnecessary.

Summary

The condition must be a boolean value; a number cannot be written as a condition.

Block curly braces are mandatory, but condition parentheses are not.

match forces you to handle all possibilities.

When if is used as an expression, the types of the arms must be the same.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code