Symptom#
Router::route("/", get(handler)) fails to compile:
error[E0277]: the trait bound `fn() -> impl Future<Output = String> {handler}: Handler<_, _>` is not satisfied
--> examples\f11.rs:12:53
|
12 | let _app: Router = Router::new().route("/", get(handler));
| --- ^^^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn() -> impl Future<Output = String> {handler}`
| |
| required by a bound introduced by this call
|
= note: Consider using `#[axum::debug_handler]` to improve the error message
...Cause#
A handler is any async function whose arguments are extractors and whose result converts into a response, and whose future can be sent between threads. When one requirement fails, the compiler can only say that the whole Handler trait is not implemented. This handler keeps an Rc (which is not Send) alive across an .await:
use axum::{routing::get, Router};
use std::rc::Rc;
async fn handler() -> String {
let counter = Rc::new(5);
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
format!("{counter}")
}
#[tokio::main]
async fn main() {
let _app: Router = Router::new().route("/", get(handler));
}Fix#
Enable the macros feature (axum = { version = "0.8.9", features = ["macros"] }) and add #[debug_handler] above the function. It checks the pieces one by one:
use axum::{debug_handler, routing::get, Router};
use std::rc::Rc;
#[debug_handler]
async fn handler() -> String {
let counter = Rc::new(5);
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
format!("{counter}")
}
#[tokio::main]
async fn main() {
let _app: Router = Router::new().route("/", get(handler));
}The error now names the cause:
error: future cannot be sent between threads safely
--> examples\f11b.rs:4:1
|
4 | #[debug_handler]
| ^^^^^^^^^^^^^^^^ future returned by `handler` is not `Send`
|
= help: within `impl Future<Output = String>`, the trait `Send` is not implemented for `Rc<i32>`
note: future is not `Send` as this value is used across an await
--> examples\f11b.rs:7:61
|
6 | let counter = Rc::new(5);
| ------- has type `Rc<i32>` which is not `Send`
7 | tokio::time::sleep(std::time::Duration::from_millis(1)).await;
| ^^^^^ await occurs here, with `counter` maybe used later
note: required by a bound in `__axum_macros_check_handler_future::check`
--> examples\f11b.rs:4:1
|
4 | #[debug_handler]
| ^^^^^^^^^^^^^^^^ required by this bound in `check`
...Replace Rc with Arc, or end the value's life before the .await by putting it in a smaller block. Remove #[debug_handler] afterwards if you like: it only improves messages.
Common mistakes#
- Holding a
std::sync::MutexGuardover an.await. It is notSendeither, and the macro reports it the same way.
Related questions#
Why must the handler's future be Send?#
The runtime may move a request's task from one worker thread to another between awaits, so everything alive across an await must be safe to move.
Sources#
- axum,
debug_handler: https://docs.rs/axum/0.8.9/axum/attr.debug_handler.html (read 2026-12-05) - Tokio tutorial, shared state (holding locks across awaits): https://tokio.rs/tokio/tutorial/shared-state (read 2026-12-05)