Struct MatchedPath

pub struct MatchedPath(/* private field */);

Access the path in the router that matches the request.

use axum::{
    Router,
    extract::MatchedPath,
    routing::get,
};

let app = Router::new().route(
    "/users/{id}",
    get(|path: MatchedPath| async move {
        let path = path.as_str();
        // `path` will be "/users/{id}"
    })
);
# let _: Router = app;

Accessing MatchedPath via extensions

MatchedPath can also be accessed from middleware via request extensions.

This is useful for example with Trace to create a span that contains the matched path:

use axum::{
    Router,
    extract::{Request, MatchedPath},
    routing::get,
};
use tower_http::trace::TraceLayer;

let app = Router::new()
    .route("/users/{id}", get(|| async { /* ... */ }))
    .layer(
        TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
            let path = if let Some(path) = req.extensions().get::<MatchedPath>() {
                path.as_str()
            } else {
                req.uri().path()
            };
            tracing::info_span!("http-request", %path)
        }),
    );
# let _: Router = app;

Implementations

impl MatchedPath

fn as_str(&self) -> &str

Returns a str representation of the path.

Trait Implementations

impl Clone for MatchedPath

fn clone(&self) -> MatchedPath

impl Debug for MatchedPath

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

impl<S> FromRequestParts<S> for MatchedPath where S: Send + Sync,

type Rejection = MatchedPathRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection>

impl<S> OptionalFromRequestParts<S> for MatchedPath where S: Send + Sync,

type Rejection = never;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Option<Self>, Self::Rejection>

Auto Trait Implementations

impl Freeze for MatchedPath

impl RefUnwindSafe for MatchedPath

impl Send for MatchedPath

impl Sync for MatchedPath

impl Unpin for MatchedPath

impl UnsafeUnpin for MatchedPath

impl UnwindSafe for MatchedPath

Blanket Implementations

impl<S, T> FromRequest<S, ViaParts> for MatchedPath where S: Send + Sync, T: FromRequestParts<S>,

type Rejection = <T as FromRequestParts<S>>::Rejection;
fn from_request(req: Request<Body>, state: &S) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for MatchedPath where ST: ?Sized, DT: ?Sized,

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for MatchedPath where ST: ?Sized, DT: ?Sized,

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> CloneToUninit for MatchedPath where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for MatchedPath

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef<T> for MatchedPath where T: Clone,

fn from_ref(input: &T) -> T

impl<T> Instrument for MatchedPath

impl<T> Read<Exclusive, BecauseExclusive> for MatchedPath where T: ?Sized,

impl<T> Same for MatchedPath

type Output = T;

impl<T> ToOwned for MatchedPath where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for MatchedPath

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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

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

fn vzip(self) -> V