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,
pat @ ("abd" | "bcde") => pat.len(),
"bc" => 3,
_ => 4,
}
};
assert_eq!(result, 3);
§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
match
expression does not match patterns after the wildcard.) - Guards are unavailable.
Macros§
- trie_
match - Generates a match expression that uses a trie structure.