Debug School

rakesh kumar
rakesh kumar

Posted on

How match is the primary decision-making tool in Rust

How Rust Replaces if / switch with match

In Rust, match is the primary decision-making tool. It doesn’t just replace switch; it covers many if/else patterns too, while adding safety and expressiveness that Java/Python don’t have.

Think of it like this:

match = if + switch + destructuring + safety checks
Enter fullscreen mode Exit fullscreen mode

Rust replaces most if / switch logic with match because match is safer, more expressive, and compiler-checked.

Once you start using match, you’ll notice:

fewer bugs

clearer logic

zero “forgotten case” errors
Enter fullscreen mode Exit fullscreen mode

Replacing switch (value-based branching)

Java switch

int day = 2;

switch (day) {
    case 1 -> System.out.println("Monday");
    case 2 -> System.out.println("Tuesday");
    default -> System.out.println("Other");
}

Rust match (same idea, safer)
fn main() {
    let day = 2;

    match day {
        1 => println!("Monday"),
        2 => println!("Tuesday"),
        _ => println!("Other"),
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Tuesday


✔ Similar to switch
✔ _ is like default
✔ Compiler checks coverage
Enter fullscreen mode Exit fullscreen mode

2️⃣

Replacing if / else if / else

Java / Python style

int x = -5;

if (x > 0) {
    System.out.println("Positive");
} else if (x < 0) {
    System.out.println("Negative");
} else {
    System.out.println("Zero");
}
Enter fullscreen mode Exit fullscreen mode

Rust using match with guards

fn main() {
    let x = -5;

    match x {
        n if n > 0 => println!("Positive"),
        n if n < 0 => println!("Negative"),
        _ => println!("Zero"),
    }
}
Enter fullscreen mode Exit fullscreen mode

Output


Negative
Enter fullscreen mode Exit fullscreen mode
✔ Replaces chained if-else
✔ More readable when cases grow
Enter fullscreen mode Exit fullscreen mode

3️⃣

Replacing null checks (Option + match)

Java

String name = null;

if (name != null) {
    System.out.println(name);
} else {
    System.out.println("No name");
}
Enter fullscreen mode Exit fullscreen mode

Rust (no null)

fn main() {
    let name: Option<String> = Some("Ashwani".to_string());

    match name {
        Some(n) => println!("{}", n),
        None => println!("No name"),
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Ashwani
Enter fullscreen mode Exit fullscreen mode
✔ No NullPointerException
✔ Missing case is a compile-time error
Enter fullscreen mode Exit fullscreen mode

4️⃣

Replacing polymorphic if instanceof chains

Java (runtime checks)

if (obj instanceof Integer i) {
    System.out.println(i);
} else if (obj instanceof String s) {
    System.out.println(s);
}

Enter fullscreen mode Exit fullscreen mode

Rust (enum + match)

enum Value {
    Int(i32),
    Text(String),
}

fn main() {
    let v = Value::Text("hello".to_string());

    match v {
        Value::Int(i) => println!("{}", i),
        Value::Text(s) => println!("{}", s),
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

hello

✔ No casting
✔ No runtime type errors
✔ Exhaustive handling
Enter fullscreen mode Exit fullscreen mode

5️⃣

Replacing error handling (Result + match)

Java (exceptions)

try {
    readFile();
} catch (Exception e) {
    System.out.println("Error");
}
Enter fullscreen mode Exit fullscreen mode
Rust (errors are values)
fn read_file(ok: bool) -> Result<String, String> {
    if ok {
        Ok("File content".to_string())
    } else {
        Err("Read failed".to_string())
    }
}

fn main() {
    match read_file(true) {
        Ok(data) => println!("{}", data),
        Err(err) => println!("Error: {}", err),
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

File content

✔ No hidden control flow
✔ Errors must be handled
Enter fullscreen mode Exit fullscreen mode

6️⃣

Why Rust prefers match over if / switch

🔥 Key advantages

Exhaustive checking

→ Compiler forces you to handle all cases
Enter fullscreen mode Exit fullscreen mode

Destructuring built-in

→ Pull data out of enums/structs safely
Enter fullscreen mode Exit fullscreen mode

One construct for many patterns

→ Values, ranges, enums, guards, errors
Enter fullscreen mode Exit fullscreen mode

No runtime surprises

7️⃣

When to use if in Rust (yes, it still exists)

Rust still uses if when:

Only two simple branches

No pattern matching needed

if x > 10 {
    println!("Big");
} else {
    println!("Small");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)