diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index fcf92324..06af699f 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -168,20 +168,26 @@ handler takes. For example ```rust -use axum::http::{Method, HeaderMap}; +use axum::{extract::State, http::{Method, HeaderMap}}; +# +# #[derive(Clone)] +# struct AppState { +# } async fn handler( // `Method` and `HeaderMap` don't consume the request body so they can - // put anywhere in the argument list + // put anywhere in the argument list (but before `body`) method: Method, headers: HeaderMap, + // `State` is also an extractor so it needs to be before `body` + State(state): State<AppState>, // `String` consumes the request body and thus must be the last extractor body: String, ) { // ... } # -# let _: axum::routing::MethodRouter = axum::routing::get(handler); +# let _: axum::routing::MethodRouter<AppState, String> = axum::routing::get(handler); ``` We get a compile error if `String` isn't the last extractor: diff --git a/axum/src/extract/state.rs b/axum/src/extract/state.rs index 2de9f868..e2307d39 100644 --- a/axum/src/extract/state.rs +++ b/axum/src/extract/state.rs @@ -46,6 +46,11 @@ use std::{ /// # let _: axum::Router = app; /// ``` /// +/// Note that `State` is an extractor, so be sure to put it before any body +/// extractors, see ["the order of extractors"][order-of-extractors]. +/// +/// [order-of-extractors]: crate::extract#the-order-of-extractors +/// /// ## Combining stateful routers /// /// Multiple [`Router`]s can be combined with [`Router::nest`] or [`Router::merge`]