60 lines
10 KiB
HTML
60 lines
10 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="`matchit`"><title>matchit - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="matchit" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate matchit</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../matchit/index.html">matchit</a><span class="version">0.7.3</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#matchit" title="`matchit`"><code>matchit</code></a><ul><li><a href="#parameters" title="Parameters">Parameters</a></li><li><a href="#routing-priority" title="Routing Priority">Routing Priority</a></li><li><a href="#how-does-it-work" title="How does it work?">How does it work?</a></li></ul></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>matchit</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/matchit/lib.rs.html#1-123">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><h2 id="matchit"><a class="doc-anchor" href="#matchit">§</a><code>matchit</code></h2>
|
||
<p><a href="https://docs.rs/matchit"><img src="https://img.shields.io/badge/docs-0.7.3-4d76ae?style=for-the-badge" alt="Documentation" /></a>
|
||
<a href="https://crates.io/crates/matchit"><img src="https://img.shields.io/crates/v/matchit?style=for-the-badge" alt="Version" /></a>
|
||
<a href="https://crates.io/crates/matchit"><img src="https://img.shields.io/crates/l/matchit?style=for-the-badge" alt="License" /></a></p>
|
||
<p>A blazing fast URL router.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>matchit::Router;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>router = Router::new();
|
||
router.insert(<span class="string">"/home"</span>, <span class="string">"Welcome!"</span>)<span class="question-mark">?</span>;
|
||
router.insert(<span class="string">"/users/:id"</span>, <span class="string">"A User"</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let </span>matched = router.at(<span class="string">"/users/978"</span>)<span class="question-mark">?</span>;
|
||
<span class="macro">assert_eq!</span>(matched.params.get(<span class="string">"id"</span>), <span class="prelude-val">Some</span>(<span class="string">"978"</span>));
|
||
<span class="macro">assert_eq!</span>(<span class="kw-2">*</span>matched.value, <span class="string">"A User"</span>);</code></pre></div><h3 id="parameters"><a class="doc-anchor" href="#parameters">§</a>Parameters</h3>
|
||
<p>Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters:</p>
|
||
<h4 id="named-parameters"><a class="doc-anchor" href="#named-parameters">§</a>Named Parameters</h4>
|
||
<p>Named parameters like <code>/:id</code> match anything until the next <code>/</code> or the end of the path:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>m = Router::new();
|
||
m.insert(<span class="string">"/users/:id"</span>, <span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert_eq!</span>(m.at(<span class="string">"/users/1"</span>)<span class="question-mark">?</span>.params.get(<span class="string">"id"</span>), <span class="prelude-val">Some</span>(<span class="string">"1"</span>));
|
||
<span class="macro">assert_eq!</span>(m.at(<span class="string">"/users/23"</span>)<span class="question-mark">?</span>.params.get(<span class="string">"id"</span>), <span class="prelude-val">Some</span>(<span class="string">"23"</span>));
|
||
<span class="macro">assert!</span>(m.at(<span class="string">"/users"</span>).is_err());
|
||
</code></pre></div><h4 id="catch-all-parameters"><a class="doc-anchor" href="#catch-all-parameters">§</a>Catch-all Parameters</h4>
|
||
<p>Catch-all parameters start with <code>*</code> and match everything after the <code>/</code>. They must always be at the <strong>end</strong> of the route:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>m = Router::new();
|
||
m.insert(<span class="string">"/*p"</span>, <span class="bool-val">true</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert_eq!</span>(m.at(<span class="string">"/foo.js"</span>)<span class="question-mark">?</span>.params.get(<span class="string">"p"</span>), <span class="prelude-val">Some</span>(<span class="string">"foo.js"</span>));
|
||
<span class="macro">assert_eq!</span>(m.at(<span class="string">"/c/bar.css"</span>)<span class="question-mark">?</span>.params.get(<span class="string">"p"</span>), <span class="prelude-val">Some</span>(<span class="string">"c/bar.css"</span>));
|
||
|
||
<span class="comment">// note that this would not match
|
||
</span><span class="macro">assert!</span>(m.at(<span class="string">"/"</span>).is_err());
|
||
</code></pre></div><h3 id="routing-priority"><a class="doc-anchor" href="#routing-priority">§</a>Routing Priority</h3>
|
||
<p>Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>m = Router::new();
|
||
m.insert(<span class="string">"/"</span>, <span class="string">"Welcome!"</span>).unwrap() ; <span class="comment">// priority: 1
|
||
</span>m.insert(<span class="string">"/about"</span>, <span class="string">"About Me"</span>).unwrap(); <span class="comment">// priority: 1
|
||
</span>m.insert(<span class="string">"/*filepath"</span>, <span class="string">"..."</span>).unwrap(); <span class="comment">// priority: 2
|
||
</span></code></pre></div><h3 id="how-does-it-work"><a class="doc-anchor" href="#how-does-it-work">§</a>How does it work?</h3>
|
||
<p>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:</p>
|
||
<div class="example-wrap"><pre class="language-text"><code>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</code></pre></div>
|
||
<p>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.</p>
|
||
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Match.html" title="struct matchit::Match">Match</a></dt><dd>A successful match consisting of the registered value
|
||
and URL parameters, returned by <a href="struct.Router.html#method.at" title="method matchit::Router::at"><code>Router::at</code></a>.</dd><dt><a class="struct" href="struct.Params.html" title="struct matchit::Params">Params</a></dt><dd>A list of parameters returned by a route match.</dd><dt><a class="struct" href="struct.ParamsIter.html" title="struct matchit::ParamsIter">Params<wbr>Iter</a></dt><dd>An iterator over the keys and values of a route’s <a href="struct.Params.html" title="struct matchit::Params">parameters</a>.</dd><dt><a class="struct" href="struct.Router.html" title="struct matchit::Router">Router</a></dt><dd>A URL router.</dd></dl><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.InsertError.html" title="enum matchit::InsertError">Insert<wbr>Error</a></dt><dd>Represents errors that can occur when inserting a new route.</dd><dt><a class="enum" href="enum.MatchError.html" title="enum matchit::MatchError">Match<wbr>Error</a></dt><dd>A failed match attempt.</dd></dl></section></div></main></body></html> |