static_map

Macro static_map 

Source
macro_rules! static_map {
    ($vis:vis $name:ident: $T:ty; { $( $key:expr => $val:expr ),* $(,)? }) => { ... };
    ($vis:vis $name:ident: $T:ty, $M:expr; { $( $key:expr => $val:expr ),* $(,)? }) => { ... };
}
Expand description

Creates a minimal perfect hash map at compile time.

Arguments:

  • $vis: Visibility modifier (e.g. pub, pub(crate), or nothing for private)
  • $name: Name of the static variable to create
  • $T: Type of the values stored in the map
  • $M: Number of buckets to use (default is half the number of entries)
  • Key-value pairs in the form { "key1" => value1, "key2" => value2, ... }

Example:

mphf::static_map!(MY_TABLE: i32; {
	"apple" => 1,
	"banana" => 2,
	"cherry" => 3,
	"date" => 4,
});

let map = MY_TABLE.as_ref();
assert_eq!(map.get("banana"), Some(&2));
assert_eq!(map.get("fig"), None);