1 unstable release

0.1.0 Sep 10, 2023

#2929 in Rust patterns

MIT/Apache

8KB
156 lines

match_to_str

Actions Status Crate license license

Converts the pattern part of the match expression into a &'static str.
It is mainly intended to be used when you want to convert a variable name into a string as is.

Usage

$ cargo add match_to_str
use match_to_str::match_to_str;

const CAT: u8 = 1;
const DOG: u8 = 2;

fn main() {
    let animal = match_to_str!(1 => {
        CAT,
        DOG,
        _,
    });
    assert_eq!(animal, "CAT");
}

expand to:

let animal = match 1 {
    CAT => "CAT",
    DOG => "DOG",
    _ => "_",
};

Contributing

This project welcomes your PR and issues. For example, fixing bugs, adding features, refactoring, etc.

No runtime deps