pub fn get(string: &str, pattern: &str) -> Result<u64, Box<Error>>Expand description
Calculates the specificity of a pattern against a given string.
This function assumes that the string provided is already a full match for the pattern.
If the pattern does not match the string, the resulting specificity will be mathematically inconsistent and meaningless for comparison purposes.
let string = "abc";
let high = get(string, "abc").unwrap();
let low = get(string, ".*").unwrap();
assert!(high > low);Examples found in repository?
More examples
examples/sort.rs (line 16)
3fn main() {
4 let string = "alice@myprovider.com";
5
6 // You must ensure that the pattern fully matches the string before calling
7 let patterns = vec![
8 r".*",
9 r"[a-z]+@[a-z]+\.[a-z]+",
10 r"alice@[a-z]+\.[a-z]+",
11 ];
12
13 let mut results: Vec<(&str, u64)> = Vec::new();
14
15 for pattern in patterns {
16 if let Ok(score) = get(string, pattern) {
17 results.push((pattern, score));
18 }
19 }
20
21 results.sort_by(|(_, a), (_, b)| b.cmp(a));
22
23 print(string, results);
24}