pub struct ServeMux { /* private fields */ }Expand description
HTTP request multiplexer. Port of Go’s http.ServeMux (Go 1.22+).
Matching rules:
- More-specific patterns beat less-specific ones (method + host > host > path).
- Literal path segments beat wildcard segments; wildcards beat tail wildcards.
- Exact path beats trailing-slash subtree of the same length.
- Among equally-specific patterns, longer paths win.
- A pattern matching path but not method returns 405 Method Not Allowed.
Implementations§
Source§impl ServeMux
impl ServeMux
pub fn new() -> Self
Sourcepub fn handle(&self, pattern: &str, handler: impl Handler + 'static)
pub fn handle(&self, pattern: &str, handler: impl Handler + 'static)
Register a handler for the given pattern.
Pattern syntax: [METHOD ][HOST]/PATH
| Example | Meaning |
|---|---|
/ | subtree catch-all (old-style) |
/api/v2/ | subtree prefix |
/items/{id} | single wildcard segment |
/files/{path...} | tail wildcard (matches rest of path) |
GET /api/users | method-restricted exact route |
example.com/ | host-restricted subtree |
GET example.com/{id} | method + host + wildcard |
Sourcepub fn handle_func<F>(&self, pattern: &str, f: F)
pub fn handle_func<F>(&self, pattern: &str, f: F)
Register a function as a handler.
Trait Implementations§
Source§impl Handler for ServeMux
impl Handler for ServeMux
fn serve_http(&self, w: &mut dyn ResponseWriter, r: &mut Request)
Auto Trait Implementations§
impl !Freeze for ServeMux
impl RefUnwindSafe for ServeMux
impl Send for ServeMux
impl Sync for ServeMux
impl Unpin for ServeMux
impl UnsafeUnpin for ServeMux
impl UnwindSafe for ServeMux
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more