match expressions

Compares switch and match; PHP match uses strict comparison, Rust match checks exhaustiveness at compile time.

C C23

C23
#include <stdio.h>
int main(void) {
    int level = 2;
    const char *label;
    switch (level) { // switch: strict matching on integers, manual break needed to prevent fall-through
        case 1:
            label = "one";
            break;
        case 2:
            label = "two";
            break;
        default:
            label = "Unknown";
    }
    printf("%s\n", label);
    return 0;
}
Output
two

C++ C++26

C++26
#include <iostream>
#include <string>
int main() {
    int level = 2;
    std::string label;
    switch (level) { // switch: strict matching on integers, manual break needed to prevent fall-through
        case 1:
            label = "one";
            break;
        case 2:
            label = "two";
            break;
        default:
            label = "Unknown";
    }
    std::cout << label << "\n";
    return 0;
}
Output
two

C# v11.0.100

v11.0.100
var level = 2;
// switch expression (C# 8+): strict matching, no fall-through, is itself an expression
var label = level switch {
    1 => "one",
    2 => "two",
    _ => "Unknown", // discard pattern as the fallback
};
Console.WriteLine(label);
Output
two

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    level := 2
    // the switch expression may be omitted: equivalent to an if-else chain evaluated clause by clause
    switch {
    case level == 1:
        fmt.Println("one")
    case level == 2:
        fmt.Println("two")
    default:
        fmt.Println("Unknown")
    }

    // plain switch terminates automatically, no fall-through; a case may list multiple values
    switch level {
    case 1, 2:
        fmt.Println("small")
    default:
        fmt.Println("big")
    }
}
Output
two
small

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        int level = 2;
        // Java 14+ switch expressions: arrow syntax, no fall-through, can be used as a value
        String label = switch (level) {
            case 1 -> "one";
            case 2 -> "two";
            default -> "Unknown"; // switch expressions must cover all branches
        };
        System.out.println(label);

        String old = "";
        switch (level) { // traditional switch statements need a manual break to prevent fall-through
            case 2:
                old = "two";
                break;
            default:
                old = "Unknown";
        }
        System.out.println(old);
    }
}
Output
two
two

JavaScript v26.13.1

v26.13.1
// JS only has the switch statement, no native match expression
const level = 2;
let label;
switch (level) { // a manual break is needed, otherwise it falls through to the next case
    case 1:
        label = "one";
        break;
    case 2:
        label = "two";
        break;
    default:
        label = "Unknown";
}
console.log(label);
Output
two

PHP v8.6.0

v8.6.0
$level = 2;
// switch: loose comparison (==), falls through without break
$label = "";
switch ($level) {
    case "2": // int 2 and string "2" are loosely equal, so it matches
        $label = "switch matched";
        break;
    default:
        $label = "switch missed";
}
echo $label, "\n";

// PHP 8 match: strict comparison (===), no fall-through, is itself an expression, arms must be exhaustive
$label2 = match ($level) {
    "2" => "match matched", // string "2" and int 2 are strictly unequal, no match
    default => "match missed",
};
echo $label2;
Output
switch matched
match missed

Python v3.15.2

v3.15.2
level = 2
match level:  # structural pattern matching (3.10+), a statement rather than an expression
    case 1:
        label = "one"
    case 2:
        label = "two"
    case _:  # wildcard arm as the fallback
        label = "Unknown"
print(label)

point = (0, 5)
match point:  # can destructure data structures, far more powerful than C-style match
    case (0, y):
        print(f"point on the Y axis, y={y}")
    case (x, 0):
        print(f"point on the X axis, x={x}")
    case (x, y):
        print(f"point ({x}, {y})")
Output
two
point on the Y axis, y=5

Rust v1.99.1

v1.99.1
fn main() {
    let level = 2;
    // match is an expression: the whole is a value, patterns must be exhaustive (or it is a compile error)
    let label = match level {
        1 => "one",
        2 => "two",
        _ => "Unknown", // wildcard arm as the fallback
    };
    println!("{}", label);

    // patterns can match ranges and destructure structures; the most powerful match among these languages
    let n = 7;
    let kind = match n {
        0 => "zero",
        1..=9 => "single digit",
        _ => "larger",
    };
    println!("{}", kind);
}
Output
two
single digit

TypeScript v26.13.1

v26.13.1
// TS has no native match expression; use a switch statement (strict comparison, manual break needed)
const level: number = 2;
let label: string;
switch (level) {
    case 1:
        label = "one";
        break;
    case 2:
        label = "two";
        break;
    default:
        label = "Unknown";
}
console.log(label);
Output
two

Language differences

C: the switch supports only strict integral matching and needs a manual break against fall-through.

C#: the 8+ switch expression matches strictly, never falls through, and is an expression, much like PHP match.

Go: switch has no implicit fall-through (an explicit fallthrough is available; no break needed) and supports a tagless switch acting as an if-else chain.

Java: 14+ switch expressions (arrow syntax) yield a value with no fall-through.

JavaScript: only the switch statement.

PHP: 8's match is an expression with strict === comparison, no fall-through, and mandatory exhaustiveness (default as the catch-all), unlike switch, which compares loosely with == and needs manual break (the example shows the two producing opposite results for the same value).

Python: 3.10+ match is structural pattern matching (a statement, not an expression) — it can destructure tuples/classes/mappings and supports guard conditions, far beyond C-style match.

Rust: match is an expression with compile-time exhaustiveness checking; patterns support ranges, destructuring, and guards — the most powerful of all.