Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.

Use | in Rust to match multiple patterns in one arm. PHP match can list multiple values per case.

Rust

fn main() {
    let x = 1;
    match x {
        1 | 2 => println!("One or two"),
        3 => println!("Three"),
        _ => println!("Other"),
    }
}

PHP

<?php
$x = 1;
echo match ($x) {
    1, 2 => "One or two",
    3 => "Three",
    default => "Other",
} . "\n";

Comparison

  • Rust: 1 | 2 means “match 1 or 2”; same arm for both.
  • PHP: 1, 2 => in match handles multiple values for one case.
  • Rust: | can combine complex patterns (e.g. enum variants).
  • PHP: Only literals or expressions; no pattern syntax.

References

What's next

Next: Adding conditional logic with match guards.

From PHP to Rust — Rust Syntax and Functions