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 (integer literals in the first key’s type shape must have suffixes; later unsuffixed integers infer from the same position in that first key)
- arrays of
u8integer literals - tuples of any supported key expressions, up to 12 elements
- dereferenced byte string literals
- OR patterns using
|to map multiple keys to the same value UniCase::unicode(string),UniCase::ascii(string), orAscii::new(string)if theunicasefeature is enabledUncasedStr::new(string)if theuncasedfeature is enabled
All keys must use the same supported key expression type as the first key.
§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");
}