Crate matchit
A high performance, zero-copy URL router.
use Router;
Parameters
The router supports dynamic route segments. These can either be named or catch-all parameters.
Named parameters like /{id} match anything until the next / or the end of the path. Note that named parameters must be followed
by a / or the end of the route. Dynamic suffixes are not currently supported.
# use Router;
#
Catch-all parameters start with * and match anything until the end of the path. They must always be at the end of the route.
# use Router;
#
The literal characters { and } may be included in a static route by escaping them with the same character. For example, the { character is escaped with {{ and the } character is escaped with }}.
# use Router;
#
Routing Priority
Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:
# use Router;
#
How does it work?
The 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.
Priority Path Value
9 \ 1
3 ├s None
2 |├earch\ 2
1 |└upport\ 3
2 ├blog\ 4
1 | └{post} None
1 | └\ 5
2 ├about-us\ 6
1 | └team\ 7
1 └contact\ 8
This 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 by the number of children with registered values, increasing the chance of choosing the correct branch of the first try.
As it turns out, this method of routing is extremely fast. See the benchmark results for details.
Structs
-
Match
A successful match consisting of the registered value
and URL parameters, returned by
Router::at. - Params A list of parameters returned by a route match.
- ParamsIter An iterator over the keys and values of a route's parameters.
- Router A zero-copy URL router.
Enums
- InsertError Represents errors that can occur when inserting a new route.
- MatchError A failed match attempt.