Crate trie_match
source ·Expand description
trie_match! {}
This macro speeds up Rust’s match expression for comparing strings by using a compact
double-array data structure.
Usage
Simply wrap the existing match expression with the trie_match! {} macro as
follows:
use trie_match::trie_match;
let x = "abd";
let result = trie_match! {
match x {
"a" => 0,
"abc" => 1,
"abd" | "bcc" => 2,
"bc" => 3,
_ => 4,
}
};
assert_eq!(result, 2);cfg attribute
Only when using Nightly Rust, this macro supports conditional compilation with
the cfg attribute. To use this feature, enable features = ["cfg_attribute"]
in your Cargo.toml.
Example
use trie_match::trie_match;
let x = "abd";
let result = trie_match! {
match x {
#[cfg(not(feature = "foo"))]
"a" => 0,
"abc" => 1,
#[cfg(feature = "bar")]
"abd" | "bcc" => 2,
"bc" => 3,
_ => 4,
}
};
assert_eq!(result, 4);Limitations
The followings are different from the normal match expression:
- Only supports strings, byte strings, and u8 slices as patterns.
- The wildcard is evaluated last. (The normal
matchexpression does not match patterns after the wildcard.) - Pattern bindings are unavailable.
- Guards are unavailable.
Macros
- Generates a match expression that uses a trie structure.