Struct rocket::http::Status[]

pub struct Status {
    pub code: u16,
}
Expand description

Structure representing an HTTP status: an integer code.

A Status should rarely be created directly. Instead, an associated constant should be used; one is declared for every status defined in the HTTP standard. If a custom status code must be created, note that it is not possible to set a custom reason phrase.

use rocket::http::Status;

// Create a status from a known constant.
let ok = Status::Ok;
assert_eq!(ok.code, 200);
assert_eq!(ok.reason(), Some("OK"));

let not_found = Status::NotFound;
assert_eq!(not_found.code, 404);
assert_eq!(not_found.reason(), Some("Not Found"));

// Or from a status code: `reason()` returns the phrase when known.
let gone = Status::new(410);
assert_eq!(gone.code, 410);
assert_eq!(gone.reason(), Some("Gone"));

// `reason()` returns `None` when unknown.
let custom = Status::new(599);
assert_eq!(custom.code, 599);
assert_eq!(custom.reason(), None);

Responding

To set a custom Status on a response, use a response::status responder, which enforces correct status-based responses. Alternatively, respond with (Status, T) where T: Responder, but beware that the response may be invalid if it requires additional headers.

use rocket::http::Status;

#[get("/")]
fn index() -> (Status, &'static str) {
    (Status::NotFound, "Hey, there's no index!")
}

Fields

code: u16

The HTTP status code associated with this status.

Implementations

impl Status

pub const Continue: Status

Status with code 100 .

pub const SwitchingProtocols: Status

Status with code 101 .

pub const Processing: Status

Status with code 102 .

pub const Ok: Status

Status with code 200 .

pub const Created: Status

Status with code 201 .

pub const Accepted: Status

Status with code 202 .

pub const NonAuthoritativeInformation: Status

Status with code 203 .

pub const NoContent: Status

Status with code 204 .

pub const ResetContent: Status

Status with code 205 .

pub const PartialContent: Status

Status with code 206 .

pub const MultiStatus: Status

Status with code 207 .

pub const AlreadyReported: Status

Status with code 208 .

pub const ImUsed: Status

Status with code 226 .

pub const MultipleChoices: Status

Status with code 300 .

pub const MovedPermanently: Status

Status with code 301 .

pub const Found: Status

Status with code 302 .

pub const SeeOther: Status

Status with code 303 .

pub const NotModified: Status

Status with code 304 .

pub const UseProxy: Status

Status with code 305 .

pub const TemporaryRedirect: Status

Status with code 307 .

pub const PermanentRedirect: Status

Status with code 308 .

pub const BadRequest: Status

Status with code 400 .

pub const Unauthorized: Status

Status with code 401 .

pub const PaymentRequired: Status

Status with code 402 .

pub const Forbidden: Status

Status with code 403 .

pub const NotFound: Status

Status with code 404 .

pub const MethodNotAllowed: Status

Status with code 405 .

pub const NotAcceptable: Status

Status with code 406 .

pub const ProxyAuthenticationRequired: Status

Status with code 407 .

pub const RequestTimeout: Status

Status with code 408 .

pub const Conflict: Status

Status with code 409 .

pub const Gone: Status

Status with code 410 .

pub const LengthRequired: Status

Status with code 411 .

pub const PreconditionFailed: Status

Status with code 412 .

pub const PayloadTooLarge: Status

Status with code 413 .

pub const UriTooLong: Status

Status with code 414 .

pub const UnsupportedMediaType: Status

Status with code 415 .

pub const RangeNotSatisfiable: Status

Status with code 416 .

pub const ExpectationFailed: Status

Status with code 417 .

pub const ImATeapot: Status

Status with code 418 .

pub const MisdirectedRequest: Status

Status with code 421 .

pub const UnprocessableEntity: Status

Status with code 422 .

pub const Locked: Status

Status with code 423 .

pub const FailedDependency: Status

Status with code 424 .

pub const UpgradeRequired: Status

Status with code 426 .

pub const PreconditionRequired: Status

Status with code 428 .

pub const TooManyRequests: Status

Status with code 429 .

pub const RequestHeaderFieldsTooLarge: Status

Status with code 431 .

pub const UnavailableForLegalReasons: Status

Status with code 451 .

pub const InternalServerError: Status

Status with code 500 .

pub const NotImplemented: Status

Status with code 501 .

pub const BadGateway: Status

Status with code 502 .

pub const ServiceUnavailable: Status

Status with code 503 .

pub const GatewayTimeout: Status

Status with code 504 .

pub const HttpVersionNotSupported: Status

Status with code 505 .

pub const VariantAlsoNegotiates: Status

Status with code 506 .

pub const InsufficientStorage: Status

Status with code 507 .

pub const LoopDetected: Status

Status with code 508 .

pub const NotExtended: Status

Status with code 510 .

pub const NetworkAuthenticationRequired: Status

Status with code 511 .

pub const fn new(code: u16) -> Status

Creates a new Status with code. This should be used only to construct non-standard HTTP statuses. Use an associated constant for standard statuses.

Example

Create a custom 299 status:

use rocket::http::Status;

let custom = Status::new(299);
assert_eq!(custom.code, 299);

pub const fn class(self) -> StatusClass

Returns the class of a given status.

Example

use rocket::http::{Status, StatusClass};

let processing = Status::Processing;
assert_eq!(processing.class(), StatusClass::Informational);

let ok = Status::Ok;
assert_eq!(ok.class(), StatusClass::Success);

let see_other = Status::SeeOther;
assert_eq!(see_other.class(), StatusClass::Redirection);

let not_found = Status::NotFound;
assert_eq!(not_found.class(), StatusClass::ClientError);

let internal_error = Status::InternalServerError;
assert_eq!(internal_error.class(), StatusClass::ServerError);

let custom = Status::new(600);
assert_eq!(custom.class(), StatusClass::Unknown);

pub const fn from_code(code: u16) -> Option<Status>

Returns a Status given a standard status code code. If code is not a known status code, None is returned.

Example

Create a Status from a known code:

use rocket::http::Status;

let not_found = Status::from_code(404);
assert_eq!(not_found, Some(Status::NotFound));

Create a Status from an unknown code:

use rocket::http::Status;

let unknown = Status::from_code(600);
assert!(unknown.is_none());

pub const fn reason(&self) -> Option<&'static str>

Returns the canonical reason phrase if self corresponds to a canonical, known status code. Otherwise, returns None.

Example

Reason phrase from a known code:

use rocket::http::Status;

assert_eq!(Status::Created.reason(), Some("Created"));
assert_eq!(Status::new(200).reason(), Some("OK"));

Absent phrase from an unknown code:

use rocket::http::Status;

assert_eq!(Status::new(499).reason(), None);

pub const fn reason_lossy(&self) -> &'static str

Returns the canonical reason phrase if self corresponds to a canonical, known status code, or an unspecified but relevant reason phrase otherwise.

Example

use rocket::http::Status;

assert_eq!(Status::NotFound.reason_lossy(), "Not Found");
assert_eq!(Status::new(100).reason_lossy(), "Continue");
assert!(!Status::new(699).reason_lossy().is_empty());

Trait Implementations

impl Clone for Status

pub fn clone(&self) -> Status

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Status

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

impl Default for Status

pub fn default() -> Status

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

impl Display for Status

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

impl Hash for Status

pub fn hash<H>(&self, state: &mut H) where
    H: Hasher

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given Hasher. Read more

impl Ord for Status

pub fn cmp(&self, other: &Status) -> Ordering

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl PartialEq<Status> for Status

pub fn eq(&self, other: &Status) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl PartialOrd<Status> for Status

pub fn partial_cmp(&self, other: &Status) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<'r> Responder<'r, 'static> for Status[src]

The response generated by Status depends on the status code itself. The table below summarizes the functionality:

Status Code RangeResponse
[400, 599]Forwards to catcher for given status.
100, [200, 205]Empty with status of self.
All others.Invalid. Errors to 500 catcher.

In short, a client or server error status codes will forward to the corresponding error catcher, a successful status code less than 206 or 100 responds with any empty body and the given status code, and all other status code emit an error message and forward to the 500 (internal server error) catcher.

fn respond_to(self, _: &'r Request<'_>) -> Result<'static>[src]

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more

impl Copy for Status

impl Eq for Status

Auto Trait Implementations

impl RefUnwindSafe for Status

impl Send for Status

impl Sync for Status

impl Unpin for Status

impl UnwindSafe for Status

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

pub fn equivalent(&self, key: &K) -> bool[src]

Compare self to key and return true if they are equal.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

pub fn into_collection<A>(self) -> SmallVec<A> where
    A: Array<Item = T>, 

Converts self into a collection.

pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
    F: FnMut(T) -> U,
    A: Array<Item = U>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

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

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 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> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

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

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

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

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V