Router

Struct Router 

Source
pub struct Router { /* private fields */ }
Expand description

HTTP router for dispatching requests to handlers.

§Example

let router = Router::new()
    .get("/", || "Hello, World!")
    .get("/users", list_users)
    .get("/users/:id", get_user)
    .post("/users", create_user)
    .put("/users/:id", update_user)
    .delete("/users/:id", delete_user);

Implementations§

Source§

impl Router

Source

pub fn new() -> Self

Creates a new empty router.

Source

pub fn route<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a route that matches any HTTP method.

Source

pub fn get<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a GET route.

Source

pub fn post<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a POST route.

Source

pub fn put<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a PUT route.

Source

pub fn delete<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a DELETE route.

Source

pub fn patch<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a PATCH route.

Source

pub fn head<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds a HEAD route.

Source

pub fn options<H, Args>(self, pattern: &str, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Adds an OPTIONS route.

Source

pub fn not_found<H, Args>(self, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Sets a custom 404 Not Found handler.

Source

pub fn method_not_allowed<H, Args>(self, handler: H) -> Self
where H: Handler<Args> + 'static, H::Output: IntoResponse,

Sets a custom 405 Method Not Allowed handler.

Source

pub fn nest(self, prefix: &str, nested: Router) -> Self

Nests another router under a path prefix.

§Example
let api_router = Router::new()
    .get("/users", list_users)
    .get("/users/:id", get_user);

let main_router = Router::new()
    .get("/", home)
    .nest("/api", api_router);
Source

pub fn handle(&self, req: &mut Request) -> Response

Handles a request and returns a response.

Source

pub fn run(self)

Runs the router, handling the current request.

This is the main entry point for using the router.

§Example
#[unsafe(no_mangle)]
pub extern "C" fn _start() {
    Router::new()
        .get("/", || "Hello!")
        .run();
}

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl !Send for Router

§

impl !Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.