Struct http::StatusCode

Overview

An HTTP status code.

Methods

fn as_u16(self) -> u64

Returns the u16 corresponding to this StatusCode.

Note

This is the same as the From<StatusCode> implementation, but included as an inherent method because that implementation doesn't appear in rustdocs, as well as a way to force the type instead of relying on inference.

Example

use http::StatusCode;

let status = StatusCode::OK;
assert_eq!(status.as_u16(), 200);
fn as_str(self) -> String

Returns a &str representation of the StatusCode

The return value only includes a numerical representation of the status code. The canonical reason is not included.

Example

use http::StatusCode;

let status = StatusCode::OK;
assert_eq!(status.as_str(), "200");

Get the standardised reason-phrase for this status code.

This is mostly here for servers writing responses, but could potentially have application at other times.

The reason phrase is defined as being exclusively for human readers. You should avoid deriving any meaning from it at all costs.

Bear in mind also that in HTTP/2.0 and HTTP/3.0 the reason phrase is abolished from transmission, and so this canonical reason phrase really is the only reason phrase you’ll find.

Example

use http::StatusCode;

let status = StatusCode::OK;
assert_eq!(status.canonical_reason(), Some("OK"));

Check if status is within 100-199.

Check if status is within 200-299.

Check if status is within 300-399.

Check if status is within 400-499.

Check if status is within 500-599.

Trait Implementations

impl PartialEq for StatusCode
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for StatusCode
impl PartialOrd for StatusCode
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

Tests less than (for self and other) and is used by the < operator.

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator.

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

Tests greater than (for self and other) and is used by the > operator.

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for StatusCode
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);

Protocols

protocol PARTIAL_EQ
if value == b { }

Test two status codes for partial equality.

Examples

use std::ops::partial_eq;
use http::StatusCode;

let ok = StatusCode::OK;
let not_found = StatusCode::NOT_FOUND;

assert_eq!(partial_eq(ok, ok), true);
assert_eq!(partial_eq(ok, not_found), false);
assert_eq!(partial_eq(not_found, ok), false);
protocol EQ
if value == b { }

Test two status codes for total equality.

Examples

use std::ops::eq;
use http::StatusCode;

let ok = StatusCode::OK;
let not_found = StatusCode::NOT_FOUND;

assert_eq!(eq(ok, ok), true);
assert_eq!(eq(ok, not_found), false);
assert_eq!(eq(not_found, ok), false);
protocol PARTIAL_CMP
if value < b { }

Perform a partial ordered comparison between two status codes.

Examples

use http::StatusCode;

let ok = StatusCode::OK;
let not_found = StatusCode::NOT_FOUND;

assert!(ok < not_found);
assert!(not_found > ok);
assert!(ok == ok);

Using explicit functions:

use std::cmp::Ordering;
use std::ops::partial_cmp;

use http::StatusCode;

let ok = StatusCode::OK;
let not_found = StatusCode::NOT_FOUND;

assert_eq!(partial_cmp(ok, not_found), Some(Ordering::Less));
assert_eq!(partial_cmp(not_found, ok), Some(Ordering::Greater));
assert_eq!(partial_cmp(ok, ok), Some(Ordering::Equal));
protocol CMP
if value < b { }

Perform a totally ordered comparison between two status codes.

Examples

use std::cmp::Ordering;
use std::ops::cmp;

use http::StatusCode;

let ok = StatusCode::OK;
let not_found = StatusCode::NOT_FOUND;

assert_eq!(cmp(ok, not_found), Ordering::Less);
assert_eq!(cmp(not_found, ok), Ordering::Greater);
assert_eq!(cmp(ok, ok), Ordering::Equal);
protocol HASH
let $out = hash(value)

Hash the status code.

Examples

use std::ops::hash;

use http::StatusCode;

let not_found = StatusCode::NOT_FOUND;

assert_eq!(hash(not_found), hash(not_found));
protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation of the status code.

Examples

use http::StatusCode;

let not_found = StatusCode::NOT_FOUND;

println!("{not_found:?}");
protocol DISPLAY_FMT
format!("{}", value)

Write a display representation of the status code.

Examples

use http::StatusCode;

let not_found = StatusCode::NOT_FOUND;

println!("{not_found}");
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.

protocol MIN
$a.min($b)

The implementation protocol for the PartialOrd::min method.

protocol MAX
$a.max($b)

The implementation protocol for the PartialOrd::max method.