1# `matchit`
2
3[![Documentation](https://img.shields.io/badge/docs-0.7.3-4d76ae?style=for-the-badge)](https://docs.rs/matchit)
4[![Version](https://img.shields.io/crates/v/matchit?style=for-the-badge)](https://crates.io/crates/matchit)
5[![License](https://img.shields.io/crates/l/matchit?style=for-the-badge)](https://crates.io/crates/matchit)
6
7A high performance, zero-copy URL router.
8
9```rust
10use matchit::Router;
11
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    let mut router = Router::new();
14    router.insert("/home", "Welcome!")?;
15    router.insert("/users/:id", "A User")?;
16
17    let matched = router.at("/users/978")?;
18    assert_eq!(matched.params.get("id"), Some("978"));
19    assert_eq!(*matched.value, "A User");
20
21    Ok(())
22}
23```
24
25## Parameters
26
27Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters:
28
29### Named Parameters
30
31Named parameters like `/:id` match anything until the next `/` or the end of the path:
32
33```rust,ignore
34let mut m = Router::new();
35m.insert("/users/:id", true)?;
36
37assert_eq!(m.at("/users/1")?.params.get("id"), Some("1"));
38assert_eq!(m.at("/users/23")?.params.get("id"), Some("23"));
39assert!(m.at("/users").is_err());
40```
41
42### Catch-all Parameters
43
44Catch-all parameters start with `*` and match everything after the `/`. They must always be at the **end** of the route:
45
46```rust,ignore
47let mut m = Router::new();
48m.insert("/*p", true)?;
49
50assert_eq!(m.at("/foo.js")?.params.get("p"), Some("foo.js"));
51assert_eq!(m.at("/c/bar.css")?.params.get("p"), Some("c/bar.css"));
52
53// note that this would not match:
54assert_eq!(m.at("/").is_err());
55```
56
57## Routing Priority
58
59Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:
60
61```rust,ignore
62let mut m = Router::new();
63m.insert("/", "Welcome!").unwrap();      // priority: 1
64m.insert("/about", "About Me").unwrap(); // priority: 1
65m.insert("/*filepath", "...").unwrap();  // priority: 2
66```
67
68## How does it work?
69
70The router takes advantage of the fact that URL routes generally follow a hierarchical structure. Routes are stored them in a radix trie that makes heavy use of common prefixes:
71
72```text
73Priority   Path             Value
749          \                1
753          ├s               None
762          |├earch\         2
771          |└upport\        3
782          ├blog\           4
791          |    └:post      None
801          |         └\     5
812          ├about-us\       6
821          |        └team\  7
831          └contact\        8
84```
85
86This allows us to reduce the route search to a small number of branches. Child nodes on the same level of the tree are also prioritized
87by the number of children with registered values, increasing the chance of choosing the correct branch of the first try.
88
89# Benchmarks
90
91As it turns out, this method of routing is extremely fast. In a benchmark matching 4 paths against 130 registered routes, `matchit` find the correct routes
92in under 200 nanoseconds, an order of magnitude faster than most other routers. You can view the benchmark code [here](https://github.com/ibraheemdev/matchit/blob/master/benches/bench.rs).
93
94```text
95Compare Routers/matchit
96time:   [197.57 ns 198.74 ns 199.83 ns]
97
98Compare Routers/actix
99time:   [26.805 us 26.811 us 26.816 us]
100
101Compare Routers/path-tree
102time:   [468.95 ns 470.34 ns 471.65 ns]
103
104Compare Routers/regex
105time:   [22.539 us 22.584 us 22.639 us]
106
107Compare Routers/route-recognizer
108time:   [3.7552 us 3.7732 us 3.8027 us]
109
110Compare Routers/routefinder
111time:   [5.7313 us 5.7405 us 5.7514 us]
112```
113
114# Credits
115
116A lot of the code in this package was based on Julien Schmidt's [`httprouter`](https://github.com/julienschmidt/httprouter).
117