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

Rust supports range patterns with ..= (inclusive). PHP match does not support range patterns; use if/elseif or conditions.

Rust

fn main() {
    let x = 5;
    match x {
        1..=5 => println!("One through five"),
        6..=10 => println!("Six through ten"),
        _ => println!("Something else"),
    }
}

PHP

<?php
$x = 5;
if ($x >= 1 && $x <= 5) {
    echo "One through five\n";
} elseif ($x >= 6 && $x <= 10) {
    echo "Six through ten\n";
} else {
    echo "Something else\n";
}

Comparison

  • Rust: a..=b matches inclusive range; works for integers and chars.
  • PHP: No range syntax in match; express as $x >= 1 && $x <= 5.
  • Rust: ..= for inclusive; .. for exclusive (half-open) in some contexts.
  • PHP: Use in_array($x, range(1, 5)) or comparison chains.

References

What's next

Next: Pattern matching with Option.

From PHP to Rust — Rust Syntax and Functions