[][src]Struct rocket::Route

pub struct Route {
    pub method: Method,
    pub handler: Handler,
    pub base: URI<'static>,
    pub uri: URI<'static>,
    pub rank: isize,
    pub format: Option<MediaType>,
}

A route: a method, its handler, path, rank, and format/media type.

Fields

The method this route matches against.

The function that should be called when the route matches.

The base mount point of this Route.

The uri (in Rocket format) that should be matched against. This uri already includes the base mount point.

The rank of this route. Lower ranks have higher priorities.

The media type this route matches against, if any.

Methods

impl Route
[src]

Creates a new route with the given method, path, and handler with a base of /.

Ranking

The route rank's is set so that routes with static paths are ranked higher than route's with dynamic paths, and routes with query strings are ranked higher than ranks without query strings. This default ranking is summarized by the table below:

static path query rank
yes yes -4
yes no -3
no yes -2
no no -1

Example

use rocket::{Request, Route, Data};
use rocket::handler::Outcome;
use rocket::http::Method;

fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> {
    Outcome::from(request, "Hello, world!")
}

// this is a rank -3 route matching requests to `GET /`
let index = Route::new(Method::Get, "/", handler);

// this is a rank -4 route matching requests to `GET /?<name>`
let index_name = Route::new(Method::Get, "/?<name>", handler);

// this is a rank -1 route matching requests to `GET /<name>`
let name = Route::new(Method::Get, "/<name>", handler);

Creates a new route with the given rank, method, path, and handler with a base of /.

Example

use rocket::{Request, Route, Data};
use rocket::handler::Outcome;
use rocket::http::Method;

fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> {
    Outcome::from(request, "Hello, world!")
}

// this is a rank 1 route matching requests to `GET /`
let index = Route::ranked(1, Method::Get, "/", handler);

Retrieves the path of the base mount point of this route as an &str.

Example

use rocket::{Request, Route, Data};
use rocket::handler::Outcome;
use rocket::http::Method;

fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> {
    Outcome::from(request, "Hello, world!")
}

let mut index = Route::ranked(1, Method::Get, "/", handler);
assert_eq!(index.base(), "/");
assert_eq!(index.base.path(), "/");

Sets the base mount point of the route. Does not update the rank or any other parameters.

Example

use rocket::{Request, Route, Data};
use rocket::handler::Outcome;
use rocket::http::Method;

fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> {
    Outcome::from(request, "Hello, world!")
}

let mut index = Route::ranked(1, Method::Get, "/", handler);
assert_eq!(index.base(), "/");
assert_eq!(index.base.path(), "/");

index.set_base("/hi");
assert_eq!(index.base(), "/hi");
assert_eq!(index.base.path(), "/hi");

Sets the path of the route. Does not update the rank or any other parameters.

Example

use rocket::{Request, Route, Data};
use rocket::handler::Outcome;
use rocket::http::Method;

fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> {
    Outcome::from(request, "Hello, world!")
}

let mut index = Route::ranked(1, Method::Get, "/", handler);
assert_eq!(index.uri.path(), "/");

index.set_uri("/hello");
assert_eq!(index.uri.path(), "/hello");

Trait Implementations

impl<'a, 'r> FromRequest<'a, 'r> for &'r Route
[src]

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

impl Clone for Route
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Route
[src]

Formats the value using the given formatter. Read more

impl Display for Route
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Route

impl Sync for Route

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

Converts the given value to a String. Read more

impl<T> ToOwned for T where
    T: Clone
[src]

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.