What is the syntax of a switch statement in Rust?

Oct 26, 2026

Leave a message

Ethan Lopez
Ethan Lopez
Ethan is a hardware assembler at Hebei Natcon. With his meticulous work, he assembles high - quality computer hardware components. His skills and dedication contribute to the production of reliable hardware products for our global customers.

As a reputable switch supplier, I've had the privilege of working closely with a wide array of networking equipment, from industry giants like Cisco to innovative players such as Huawei and H3C. In the world of programming, specifically in Rust, a language known for its safety, performance, and concurrency features, the switch statement takes on a slightly different form compared to other languages. In Rust, the equivalent construct is the match statement, which is both powerful and expressive.

Understanding the Basics of Rust's match Statement

The match statement in Rust is a powerful control flow construct that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. It's similar to the switch statement in languages like C, Java, or JavaScript, but it offers more flexibility and safety.

Let's take a look at a simple example:

fn main() {
    let number = 2;

    match number {
        1 => println!("The number is 1"),
        2 => println!("The number is 2"),
        3 => println!("The number is 3"),
        _ => println!("The number is something else"),
    }
}

In this example, we have a variable number with a value of 2. The match statement then compares the value of number against a series of patterns. The first pattern 1 does not match, but the second pattern 2 does. As a result, the code associated with that pattern, println!("The number is 2"), is executed.

The _ pattern is a catch-all pattern that matches any value. It's often used at the end of a match statement to handle cases that don't match any of the previous patterns.

Pattern Matching with Enums

One of the most powerful features of Rust's match statement is its ability to perform pattern matching on enums. Enums in Rust are a way to define a type by enumerating its possible variants.

Let's consider an example where we have an enum representing different types of network switches:

enum SwitchType {
    Cisco,
    Huawei,
    H3C,
}

fn main() {
    let my_switch = SwitchType::Cisco;

    match my_switch {
        SwitchType::Cisco => println!("This is a Cisco switch"),
        SwitchType::Huawei => println!("This is a Huawei switch"),
        SwitchType::H3C => println!("This is a H3C switch"),
    }
}

In this example, we define an enum SwitchType with three variants: Cisco, Huawei, and H3C. We then create a variable my_switch of type SwitchType and assign it the value SwitchType::Cisco. The match statement compares the value of my_switch against the different variants of the SwitchType enum and executes the corresponding code based on the match.

Guards in match Statements

Rust's match statement also supports guards, which are additional conditions that must be true for a pattern to match. Guards are specified using the if keyword.

Let's consider an example where we want to perform different actions based on the port number of a switch:

fn handle_port(port: u8) {
    match port {
        0..=10 if port % 2 == 0 => println!("This is an even-numbered port between 0 and 10"),
        0..=10 => println!("This is an odd-numbered port between 0 and 10"),
        _ => println!("This port is outside the range 0 to 10"),
    }
}

fn main() {
    handle_port(4);
}

In this example, the first pattern 0..=10 if port % 2 == 0 matches if the port value is between 0 and 10 (inclusive) and is an even number. The second pattern 0..=10 matches if the port value is between 0 and 10 and is an odd number. The _ pattern matches any other value.

match Statements and Exhaustiveness

One of the key safety features of Rust's match statement is its exhaustiveness checking. Rust ensures that all possible values of the matched value are covered by the patterns in the match statement. If there are any possible values that are not covered, the code will not compile.

For example, consider the following code:

enum Direction {
    North,
    South,
    East,
    West,
}

fn main() {
    let direction = Direction::North;

    match direction {
        Direction::North => println!("You are facing north"),
        Direction::South => println!("You are facing south"),
        // Missing patterns for East and West
    }
}

This code will not compile because the match statement does not cover all possible variants of the Direction enum. To fix this, we can add the missing patterns or use the _ catch-all pattern:

enum Direction {
    North,
    South,
    East,
    West,
}

fn main() {
    let direction = Direction::North;

    match direction {
        Direction::North => println!("You are facing north"),
        Direction::South => println!("You are facing south"),
        Direction::East => println!("You are facing east"),
        Direction::West => println!("You are facing west"),
    }
}

Our Offering as a Switch Supplier

As a leading switch supplier, we offer a wide range of networking switches to meet the diverse needs of our customers. Our product portfolio includes high-quality switches from top manufacturers such as Cisco, Huawei, and H3C.

H3C S5560X suppliersH3C S5560X

One of our popular products is the Switch Cisco C1000 24t 4g L. This Cisco switch is known for its reliability and performance, making it an ideal choice for small to medium-sized businesses.

We also offer the Huawei S6720 30C EI 24S AC, a powerful switch from Huawei that provides high-speed connectivity and advanced features for enterprise networks.

Another great option is the H3C S5560X, a versatile switch from H3C that offers excellent performance and flexibility for different networking environments.

Contact Us for Switch Procurement

If you're in the market for high-quality networking switches, we'd love to hear from you. Our team of experts is ready to assist you in finding the right switches for your specific needs. Whether you're a small business looking for a reliable switch or a large enterprise in need of a high-performance solution, we have the products and expertise to meet your requirements.

Feel free to reach out to us to start the procurement process and take your network to the next level.

References

  • The Rust Programming Language, Steve Klabnik and Carol Nichols.
  • Rust by Example, Various contributors.
Send Inquiry
Contact usif have any question

You can either contact us via phone, email or online form below. Our specialist will contact you back shortly.

Contact now!