Logo

dev-resources.site

for different kinds of informations.

Compact `match` in Rust

Published at
9/17/2023
Categories
rust
optimization
code
formatting
Author
rsk
Categories
4 categories in total
rust
open
optimization
open
code
open
formatting
open
Author
3 person written this
rsk
open
Compact `match` in Rust

match often used to work with enums, let's look at this example:

enum NextStep {
    TurnLeft,
    TurnRight
}

fn main() {
    let next_step = NextStep::TurnLeft;
    match next_step {
        NextStep::TurnLeft => println!("turn left"),
        NextStep::TurnRight => println!("turn right"),
    }
}
Enter fullscreen mode Exit fullscreen mode

It may not be obvious, but enums in Rust can be brought into the current namespace, allowing for more compact code:

// ...
    use NextStep::*;
    match next_step {
        TurnLeft => println!("turn left"),
        TurnRight => println!("turn right"),
    }
// ...
Enter fullscreen mode Exit fullscreen mode

And, to limit the effect to just match, you will have to set boundaries using {} for an additional scope:

// ...
    {
        use NextStep::*;
        match next_step {
            // ...
        }
    }
// ...
Enter fullscreen mode Exit fullscreen mode

My links

I'm making my perfect todo, note-taking app Heaplist, you can try it here heaplist.app
And I have a twitter @rsk

formatting Article's
30 articles in total
Favicon
How to Configure VSCode for Auto Formatting and Linting in Python
Favicon
Clean Code: Open Source Linting & Formatting
Favicon
A One-Liner `sed` Command to Format SSH Config File
Favicon
Developing a Custom Gradle Plugin for Formatting and Static Analysis
Favicon
Why Do I Love Code Formatters?
Favicon
My opinion about opinionated Prettier: 👎
Favicon
How to convert XML files to CSV format using Boomi in Docker
Favicon
Format Time Ago Function - Time Differences in TypeScript
Favicon
How to use Prettier as a Github Action
Favicon
My universal code beautification tool
Favicon
What are formatting tags in HTML?
Favicon
Compact `match` in Rust
Favicon
Axis Headaches? Examples for Formatting Tick Labels (Matplotlib)
Favicon
Wednesday Links - Edition 2023-05-17
Favicon
Formatting External Drives On Linux Using Gparted.
Favicon
Make Your Code Shine with Prettier Extension for VS Code
Favicon
Accounting Number Format in Excel – How to Apply it to Selected Cells
Favicon
How to Clear Formatting in Excel – Remove Format From a Cell
Favicon
Best Practice #1 : One Function can be accessed many times with Different Values
Favicon
Hugo.io - Multiline cells in a table
Favicon
AppVeyor and python formatting
Favicon
Checking your python code format on Azure Pipelines
Favicon
Formatting Python – Why and How !
Favicon
Golang automatic code formatting : Code like a Pro
Favicon
Clean Code in C# Part 4 Formatting
Favicon
Formatting numbers in JavaScript
Favicon
How to format relative dates using native JavaScript
Favicon
Formatting dates in JavaScript using the user's locale
Favicon
Set Cell Styles and Formatting in Excel with Java
Favicon
Code formatting for C# projects: dotnet format + GitHub Actions

Featured ones: