rustrust-tokiorust-axum

The Axum framework in Rust is unable to compile the code, following the official tutorial


When using Rust's Axum,as per the official example to use post and route, and write the route handle functions into controller mod , I encountered errors.

use axum::response::IntoResponse;
use axum::routing::{get, post};
use tokio::net::TcpListener;

use crate::model::user::User;
async fn register_handler(
    Json(user):Json<User>
) -> impl IntoResponse{
    println!("Received registration request for user: {:?}", user);
}
pub async fn init(){
    let app = Router::new()
        .route("/users/register", post(register_handler));
    let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}  

The message error is :

the trait bound `fn(axum::Json<User>) -> impl Future<Output = impl IntoResponse> {register_handler}: Handler<_, _>` is not satisfied

I have tried almost every method, whether it's changing the return value or asking ChatGPT.


Solution

  • When using Rust's Axum,as per the official example to use post and route

    That doesn't look like an official example. Official examples are generally complete, which this is not in any way.

    I have tried almost every method, whether it's changing the return value

    Changing it to what? Why are you even bothering with -> impl IntoResponse here? You're literally not returning anything, that should work fine.

    Have you tried setting #[debug_handler]? Have you checked that User is Deserializable?

    or asking ChatGPT.

    That's about as useful as hitting yourself in the nuts with a baseball bat.