Struct MatchedPath

struct MatchedPath(_)

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: &Self) -> &str

Returns a str representation of the path.

impl Clone for MatchedPath

fn clone(self: &Self) -> MatchedPath

impl Debug for MatchedPath

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

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

impl<S> FromRequestParts for MatchedPath

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, <Self as >::Rejection>

impl<S> OptionalFromRequestParts for MatchedPath

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

impl<S, T> FromRequest for MatchedPath

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

impl<T> Any for MatchedPath

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for MatchedPath

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for MatchedPath

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

impl<T> CloneToUninit for MatchedPath

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

impl<T> From for MatchedPath

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef for MatchedPath

fn from_ref(input: &T) -> T

impl<T> Instrument for MatchedPath

impl<T> Same for MatchedPath

impl<T> ToOwned for MatchedPath

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> WithSubscriber for MatchedPath

impl<T, U> Into for MatchedPath

fn into(self: 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 for MatchedPath

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

impl<T, U> TryInto for MatchedPath

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

impl<V, T> VZip for MatchedPath

fn vzip(self: Self) -> V