phf_map

Macro phf_map 

Source
phf_map!() { /* proc-macro */ }
Expand description

Macro to create a static (compile-time) Map.

Requires the macros feature.

Supported key expressions are:

  • literals: bools, (byte) strings, bytes, chars, and integers (these must have a type suffix)
  • arrays of u8 integers
  • tuples of any supported key expressions
  • dereferenced byte string literals
  • OR patterns using | to map multiple keys to the same value
  • UniCase::unicode(string), UniCase::ascii(string), or Ascii::new(string) if the unicase feature is enabled
  • UncasedStr::new(string) if the uncased feature is enabled

§Example

use phf::{phf_map, Map};

static MY_MAP: Map<&'static str, u32> = phf_map! {
    "hello" => 1,
    "world" => 2,
};

fn main () {
    assert_eq!(MY_MAP["hello"], 1);
}

§OR Patterns

You can use OR patterns to map multiple keys to the same value:

use phf::{phf_map, Map};

static OPERATORS: Map<&'static str, &'static str> = phf_map! {
    "+" | "add" | "plus" => "addition",
    "-" | "sub" | "minus" => "subtraction",
    "*" | "mul" | "times" => "multiplication",
};

fn main() {
    assert_eq!(OPERATORS["+"], "addition");
    assert_eq!(OPERATORS["add"], "addition");
    assert_eq!(OPERATORS["plus"], "addition");
}