expression
Axum: typed path parameters
A named route segment can be parsed into a Rust value. Axum 0.8 uses braces for captures. Invalid numeric segments are rejected before this handler executes. In-memory standalone Cargo example. No socket is opened. Expected stdout (not executed): 200 item 42 400 Common mistake: Using the old /items/:id syntax or accepting a parsed identifier as proof of authorization.
Formal Syntax & Structure
rust syntaxrust
Path(id): Path<u32>; /items/{id}Working Example
Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use axum::{routing::get, extract::Path}; async fn item(Path(id): Path<u32>) -> String { format!("item {id}") } let app = Router::new().route("/items/{id}", get(item)); for uri in ["/items/42", "/items/nope"] { let r = app.clone().oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap()).await.unwrap(); println!("{}", r.status().as_u16()); if r.status().is_success() { println!("{}", std::str::from_utf8(&to_bytes(r.into_body(), 64).await.unwrap()).unwrap()); } }}