expression

Axum: multiple path captures

Use a tuple when a route has two ordered captures. The tuple types must correspond to the captured values. For more complex routes, named deserialization can be clearer. In-memory standalone Cargo example. No socket is opened. Expected stdout (not executed): books/7 Common mistake: Reversing the tuple order or extracting the same captures with multiple Path arguments.

Formal Syntax & Structure

rust syntaxrust
Path((group, item)): Path<(String, u32)>

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 show(Path((group, id)): Path<(String, u32)>) -> String { format!("{group}/{id}") }    let app = Router::new().route("/groups/{group}/items/{id}", get(show));    let r = app.oneshot(Request::builder().uri("/groups/books/items/7").body(Body::empty()).unwrap()).await.unwrap();    println!("{}", std::str::from_utf8(&to_bytes(r.into_body(), 64).await.unwrap()).unwrap());}
Rust Reference