How to join! with axum::serve::Serve future?

Hello,
I just started to learn rust :slight_smile: and working on a small web service application based on axum.

I'm working with future for the synchronous implementation and get a Serve<Router,Router> object which is the Future according to the documentation: Serve in axum::serve - Rust

Unfortunately the compiler doesn't accept this future within the join! macro:

Serve<Router, Router> is not a future

Here is a minimal example:

async fn hello() -> String {
    return "hello".to_string()
}

async fn world() -> String {
    return "world".to_string()
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    println!("Hello, world!");

    let a = axum::serve(
        tokio::net::TcpListener::bind("0.0.0.0:4242").await.unwrap(), 
        Router::new().route("/hello", get( hello))
    );

    let b = axum::serve(
        tokio::net::TcpListener::bind("0.0.0.0:4243").await.unwrap(), 
        Router::new().route("/world", get( world))
    );

    //a.await.unwrap();
    //b.await.unwrap();

    tokio::join!(a, b);

    Ok(())
}

Does anyone has a hint, what I'm doing wrong? And what I'm misunderstanding in the documentation where axum::serve::Serve is described as the Future...?

Thanks

I think you have to call Serve::into_future to convert it to a type that implements Future.


Edit: indeed, it compiles. But probably even better is to spawn a task for each server.

Hi @jofas

Wow, many thanks for the answer. As well as for the idea of using spawn. That feels pretty good!

And https://www.rustexplorer.com/ is also new for me. That's really nice to share a minimal working example :slight_smile:

Thanks for all that good stuff and merry Christmas

1 Like

Merry Christmas to you as well :slightly_smiling_face: